github.com/google/go-github/v68@v68.0.0/github/orgs_credential_authorizations_test.go (about)

     1  // Copyright 2023 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  	"time"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestOrganizationsService_ListCredentialAuthorizations(t *testing.T) {
    19  	t.Parallel()
    20  	client, mux, _ := setup(t)
    21  
    22  	mux.HandleFunc("/orgs/o/credential-authorizations", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, http.MethodGet)
    24  		testFormValues(t, r, values{"per_page": "2", "page": "2", "login": "l"})
    25  		fmt.Fprint(w, `[
    26  			{
    27  				"login": "l",
    28  				"credential_id": 1,
    29  				"credential_type": "t",
    30  				"credential_authorized_at": "2017-01-21T00:00:00Z",
    31  				"credential_accessed_at": "2017-01-21T00:00:00Z",
    32  				"authorized_credential_id": 1
    33  			}
    34  		]`)
    35  	})
    36  
    37  	opts := &CredentialAuthorizationsListOptions{
    38  		ListOptions: ListOptions{Page: 2, PerPage: 2},
    39  		Login:       "l",
    40  	}
    41  
    42  	ctx := context.Background()
    43  	creds, _, err := client.Organizations.ListCredentialAuthorizations(ctx, "o", opts)
    44  	if err != nil {
    45  		t.Errorf("Organizations.ListCredentialAuthorizations returned error: %v", err)
    46  	}
    47  
    48  	ts := time.Date(2017, time.January, 21, 0, 0, 0, 0, time.UTC)
    49  	want := []*CredentialAuthorization{
    50  		{
    51  			Login:                  Ptr("l"),
    52  			CredentialID:           Ptr(int64(1)),
    53  			CredentialType:         Ptr("t"),
    54  			CredentialAuthorizedAt: &Timestamp{ts},
    55  			CredentialAccessedAt:   &Timestamp{ts},
    56  			AuthorizedCredentialID: Ptr(int64(1)),
    57  		},
    58  	}
    59  	if !cmp.Equal(creds, want) {
    60  		t.Errorf("Organizations.ListCredentialAuthorizations returned %+v, want %+v", creds, want)
    61  	}
    62  
    63  	const methodName = "ListCredentialAuthorizations"
    64  	testBadOptions(t, methodName, func() (err error) {
    65  		_, _, err = client.Organizations.ListCredentialAuthorizations(ctx, "\n", opts)
    66  		return err
    67  	})
    68  
    69  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    70  		_, resp, err := client.Organizations.ListCredentialAuthorizations(ctx, "o", opts)
    71  		return resp, err
    72  	})
    73  }
    74  
    75  func TestOrganizationsService_RemoveCredentialAuthorization(t *testing.T) {
    76  	t.Parallel()
    77  	client, mux, _ := setup(t)
    78  
    79  	mux.HandleFunc("/orgs/o/credential-authorizations/1", func(w http.ResponseWriter, r *http.Request) {
    80  		testMethod(t, r, http.MethodDelete)
    81  		w.WriteHeader(http.StatusNoContent)
    82  	})
    83  
    84  	ctx := context.Background()
    85  	resp, err := client.Organizations.RemoveCredentialAuthorization(ctx, "o", 1)
    86  	if err != nil {
    87  		t.Errorf("Organizations.RemoveCredentialAuthorization returned error: %v", err)
    88  	}
    89  
    90  	if resp.StatusCode != http.StatusNoContent {
    91  		t.Errorf("Organizations.RemoveCredentialAuthorization returned %v, want %v", resp.StatusCode, http.StatusNoContent)
    92  	}
    93  
    94  	const methodName = "RemoveCredentialAuthorization"
    95  	testBadOptions(t, methodName, func() (err error) {
    96  		_, err = client.Organizations.RemoveCredentialAuthorization(ctx, "\n", 0)
    97  		return err
    98  	})
    99  
   100  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   101  		return client.Organizations.RemoveCredentialAuthorization(ctx, "o", 1)
   102  	})
   103  }