github.com/google/go-github/v33@v33.0.0/github/licenses_test.go (about)

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"net/http"
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  func TestRepositoryLicense_marshal(t *testing.T) {
    17  	testJSONMarshal(t, &RepositoryLicense{}, "{}")
    18  
    19  	rl := &RepositoryLicense{
    20  		Name:        String("n"),
    21  		Path:        String("p"),
    22  		SHA:         String("s"),
    23  		Size:        Int(1),
    24  		URL:         String("u"),
    25  		HTMLURL:     String("h"),
    26  		GitURL:      String("g"),
    27  		DownloadURL: String("d"),
    28  		Type:        String("t"),
    29  		Content:     String("c"),
    30  		Encoding:    String("e"),
    31  		License: &License{
    32  			Key:            String("k"),
    33  			Name:           String("n"),
    34  			URL:            String("u"),
    35  			SPDXID:         String("s"),
    36  			HTMLURL:        String("h"),
    37  			Featured:       Bool(true),
    38  			Description:    String("d"),
    39  			Implementation: String("i"),
    40  			Permissions:    &[]string{"p"},
    41  			Conditions:     &[]string{"c"},
    42  			Limitations:    &[]string{"l"},
    43  			Body:           String("b"),
    44  		},
    45  	}
    46  	want := `{
    47  		"name": "n",
    48  		"path": "p",
    49  		"sha": "s",
    50  		"size": 1,
    51  		"url": "u",
    52  		"html_url": "h",
    53  		"git_url": "g",
    54  		"download_url": "d",
    55  		"type": "t",
    56  		"content": "c",
    57  		"encoding": "e",
    58  		"license": {
    59  			"key": "k",
    60  			"name": "n",
    61  			"url": "u",
    62  			"spdx_id": "s",
    63  			"html_url": "h",
    64  			"featured": true,
    65  			"description": "d",
    66  			"implementation": "i",
    67  			"permissions": ["p"],
    68  			"conditions": ["c"],
    69  			"limitations": ["l"],
    70  			"body": "b"
    71  		}
    72  	}`
    73  	testJSONMarshal(t, rl, want)
    74  }
    75  
    76  func TestLicense_marshal(t *testing.T) {
    77  	testJSONMarshal(t, &License{}, "{}")
    78  
    79  	l := &License{
    80  		Key:            String("k"),
    81  		Name:           String("n"),
    82  		URL:            String("u"),
    83  		SPDXID:         String("s"),
    84  		HTMLURL:        String("h"),
    85  		Featured:       Bool(true),
    86  		Description:    String("d"),
    87  		Implementation: String("i"),
    88  		Permissions:    &[]string{"p"},
    89  		Conditions:     &[]string{"c"},
    90  		Limitations:    &[]string{"l"},
    91  		Body:           String("b"),
    92  	}
    93  	want := `{
    94  		"key": "k",
    95  		"name": "n",
    96  		"url": "u",
    97  		"spdx_id": "s",
    98  		"html_url": "h",
    99  		"featured": true,
   100  		"description": "d",
   101  		"implementation": "i",
   102  		"permissions": ["p"],
   103  		"conditions": ["c"],
   104  		"limitations": ["l"],
   105  		"body": "b"
   106  	}`
   107  	testJSONMarshal(t, l, want)
   108  }
   109  
   110  func TestLicensesService_List(t *testing.T) {
   111  	client, mux, _, teardown := setup()
   112  	defer teardown()
   113  
   114  	mux.HandleFunc("/licenses", func(w http.ResponseWriter, r *http.Request) {
   115  		testMethod(t, r, "GET")
   116  		fmt.Fprint(w, `[{"key":"mit","name":"MIT","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","featured":true}]`)
   117  	})
   118  
   119  	licenses, _, err := client.Licenses.List(context.Background())
   120  	if err != nil {
   121  		t.Errorf("Licenses.List returned error: %v", err)
   122  	}
   123  
   124  	want := []*License{{
   125  		Key:      String("mit"),
   126  		Name:     String("MIT"),
   127  		SPDXID:   String("MIT"),
   128  		URL:      String("https://api.github.com/licenses/mit"),
   129  		Featured: Bool(true),
   130  	}}
   131  	if !reflect.DeepEqual(licenses, want) {
   132  		t.Errorf("Licenses.List returned %+v, want %+v", licenses, want)
   133  	}
   134  }
   135  
   136  func TestLicensesService_Get(t *testing.T) {
   137  	client, mux, _, teardown := setup()
   138  	defer teardown()
   139  
   140  	mux.HandleFunc("/licenses/mit", func(w http.ResponseWriter, r *http.Request) {
   141  		testMethod(t, r, "GET")
   142  		fmt.Fprint(w, `{"key":"mit","name":"MIT"}`)
   143  	})
   144  
   145  	license, _, err := client.Licenses.Get(context.Background(), "mit")
   146  	if err != nil {
   147  		t.Errorf("Licenses.Get returned error: %v", err)
   148  	}
   149  
   150  	want := &License{Key: String("mit"), Name: String("MIT")}
   151  	if !reflect.DeepEqual(license, want) {
   152  		t.Errorf("Licenses.Get returned %+v, want %+v", license, want)
   153  	}
   154  }
   155  
   156  func TestLicensesService_Get_invalidTemplate(t *testing.T) {
   157  	client, _, _, teardown := setup()
   158  	defer teardown()
   159  
   160  	_, _, err := client.Licenses.Get(context.Background(), "%")
   161  	testURLParseError(t, err)
   162  }