github.com/google/go-github/v70@v70.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  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestRepositoryLicense_Marshal(t *testing.T) {
    18  	t.Parallel()
    19  	testJSONMarshal(t, &RepositoryLicense{}, "{}")
    20  
    21  	rl := &RepositoryLicense{
    22  		Name:        Ptr("n"),
    23  		Path:        Ptr("p"),
    24  		SHA:         Ptr("s"),
    25  		Size:        Ptr(1),
    26  		URL:         Ptr("u"),
    27  		HTMLURL:     Ptr("h"),
    28  		GitURL:      Ptr("g"),
    29  		DownloadURL: Ptr("d"),
    30  		Type:        Ptr("t"),
    31  		Content:     Ptr("c"),
    32  		Encoding:    Ptr("e"),
    33  		License: &License{
    34  			Key:            Ptr("k"),
    35  			Name:           Ptr("n"),
    36  			URL:            Ptr("u"),
    37  			SPDXID:         Ptr("s"),
    38  			HTMLURL:        Ptr("h"),
    39  			Featured:       Ptr(true),
    40  			Description:    Ptr("d"),
    41  			Implementation: Ptr("i"),
    42  			Permissions:    &[]string{"p"},
    43  			Conditions:     &[]string{"c"},
    44  			Limitations:    &[]string{"l"},
    45  			Body:           Ptr("b"),
    46  		},
    47  	}
    48  	want := `{
    49  		"name": "n",
    50  		"path": "p",
    51  		"sha": "s",
    52  		"size": 1,
    53  		"url": "u",
    54  		"html_url": "h",
    55  		"git_url": "g",
    56  		"download_url": "d",
    57  		"type": "t",
    58  		"content": "c",
    59  		"encoding": "e",
    60  		"license": {
    61  			"key": "k",
    62  			"name": "n",
    63  			"url": "u",
    64  			"spdx_id": "s",
    65  			"html_url": "h",
    66  			"featured": true,
    67  			"description": "d",
    68  			"implementation": "i",
    69  			"permissions": ["p"],
    70  			"conditions": ["c"],
    71  			"limitations": ["l"],
    72  			"body": "b"
    73  		}
    74  	}`
    75  	testJSONMarshal(t, rl, want)
    76  }
    77  
    78  func TestLicense_Marshal(t *testing.T) {
    79  	t.Parallel()
    80  	testJSONMarshal(t, &License{}, "{}")
    81  
    82  	l := &License{
    83  		Key:            Ptr("k"),
    84  		Name:           Ptr("n"),
    85  		URL:            Ptr("u"),
    86  		SPDXID:         Ptr("s"),
    87  		HTMLURL:        Ptr("h"),
    88  		Featured:       Ptr(true),
    89  		Description:    Ptr("d"),
    90  		Implementation: Ptr("i"),
    91  		Permissions:    &[]string{"p"},
    92  		Conditions:     &[]string{"c"},
    93  		Limitations:    &[]string{"l"},
    94  		Body:           Ptr("b"),
    95  	}
    96  	want := `{
    97  		"key": "k",
    98  		"name": "n",
    99  		"url": "u",
   100  		"spdx_id": "s",
   101  		"html_url": "h",
   102  		"featured": true,
   103  		"description": "d",
   104  		"implementation": "i",
   105  		"permissions": ["p"],
   106  		"conditions": ["c"],
   107  		"limitations": ["l"],
   108  		"body": "b"
   109  	}`
   110  	testJSONMarshal(t, l, want)
   111  }
   112  
   113  func TestLicensesService_List(t *testing.T) {
   114  	t.Parallel()
   115  	client, mux, _ := setup(t)
   116  
   117  	mux.HandleFunc("/licenses", func(w http.ResponseWriter, r *http.Request) {
   118  		testMethod(t, r, "GET")
   119  		fmt.Fprint(w, `[{"key":"mit","name":"MIT","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","featured":true}]`)
   120  	})
   121  
   122  	ctx := context.Background()
   123  	licenses, _, err := client.Licenses.List(ctx)
   124  	if err != nil {
   125  		t.Errorf("Licenses.List returned error: %v", err)
   126  	}
   127  
   128  	want := []*License{{
   129  		Key:      Ptr("mit"),
   130  		Name:     Ptr("MIT"),
   131  		SPDXID:   Ptr("MIT"),
   132  		URL:      Ptr("https://api.github.com/licenses/mit"),
   133  		Featured: Ptr(true),
   134  	}}
   135  	if !cmp.Equal(licenses, want) {
   136  		t.Errorf("Licenses.List returned %+v, want %+v", licenses, want)
   137  	}
   138  
   139  	const methodName = "List"
   140  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   141  		got, resp, err := client.Licenses.List(ctx)
   142  		if got != nil {
   143  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   144  		}
   145  		return resp, err
   146  	})
   147  }
   148  
   149  func TestLicensesService_Get(t *testing.T) {
   150  	t.Parallel()
   151  	client, mux, _ := setup(t)
   152  
   153  	mux.HandleFunc("/licenses/mit", func(w http.ResponseWriter, r *http.Request) {
   154  		testMethod(t, r, "GET")
   155  		fmt.Fprint(w, `{"key":"mit","name":"MIT"}`)
   156  	})
   157  
   158  	ctx := context.Background()
   159  	license, _, err := client.Licenses.Get(ctx, "mit")
   160  	if err != nil {
   161  		t.Errorf("Licenses.Get returned error: %v", err)
   162  	}
   163  
   164  	want := &License{Key: Ptr("mit"), Name: Ptr("MIT")}
   165  	if !cmp.Equal(license, want) {
   166  		t.Errorf("Licenses.Get returned %+v, want %+v", license, want)
   167  	}
   168  
   169  	const methodName = "Get"
   170  	testBadOptions(t, methodName, func() (err error) {
   171  		_, _, err = client.Licenses.Get(ctx, "\n")
   172  		return err
   173  	})
   174  
   175  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   176  		got, resp, err := client.Licenses.Get(ctx, "mit")
   177  		if got != nil {
   178  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   179  		}
   180  		return resp, err
   181  	})
   182  }
   183  
   184  func TestLicensesService_Get_invalidTemplate(t *testing.T) {
   185  	t.Parallel()
   186  	client, _, _ := setup(t)
   187  
   188  	ctx := context.Background()
   189  	_, _, err := client.Licenses.Get(ctx, "%")
   190  	testURLParseError(t, err)
   191  }