| 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 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 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"}) |
| 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 := &ListOptions{Page: 2, PerPage: 2} |
| 38 |
ctx := context.Background() |
| 39 |
creds, _, err := client.Organizations.ListCredentialAuthorizations(ctx, "o", opts) |
| 40 |
if err != nil { |
| 41 |
t.Errorf("Organizations.ListCredentialAuthorizations returned error: %v", err) |
| 42 |
} |
| 43 |
|
| 44 |
ts := time.Date(2017, time.January, 21, 0, 0, 0, 0, time.UTC) |
| 45 |
want := []*CredentialAuthorization{ |
| 46 |
{ |
| 47 |
Login: String("l"), |
| 48 |
CredentialID: Int64(1), |
| 49 |
CredentialType: String("t"), |
| 50 |
CredentialAuthorizedAt: &Timestamp{ts}, |
| 51 |
CredentialAccessedAt: &Timestamp{ts}, |
| 52 |
AuthorizedCredentialID: Int64(1), |
| 53 |
}, |
| 54 |
} |
| 55 |
if !cmp.Equal(creds, want) { |
| 56 |
t.Errorf("Organizations.ListCredentialAuthorizations returned %+v, want %+v", creds, want) |
| 57 |
} |
| 58 |
|
| 59 |
const methodName = "ListCredentialAuthorizations" |
| 60 |
testBadOptions(t, methodName, func() (err error) { |
| 61 |
_, _, err = client.Organizations.ListCredentialAuthorizations(ctx, "\n", opts) |
| 62 |
return err |
| 63 |
}) |
| 64 |
|
| 65 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 66 |
_, resp, err := client.Organizations.ListCredentialAuthorizations(ctx, "o", opts) |
| 67 |
return resp, err |
| 68 |
}) |
| 69 |
} |
| 70 |
|
| 71 |
func TestOrganizationsService_RemoveCredentialAuthorization(t *testing.T) { |
| 72 |
client, mux, _, teardown := setup() |
| 73 |
defer teardown() |
| 74 |
|
| 75 |
mux.HandleFunc("/orgs/o/credential-authorizations/1", func(w http.ResponseWriter, r *http.Request) { |
| 76 |
testMethod(t, r, http.MethodDelete) |
| 77 |
w.WriteHeader(http.StatusNoContent) |
| 78 |
}) |
| 79 |
|
| 80 |
ctx := context.Background() |
| 81 |
resp, err := client.Organizations.RemoveCredentialAuthorization(ctx, "o", 1) |
| 82 |
if err != nil { |
| 83 |
t.Errorf("Organizations.RemoveCredentialAuthorization returned error: %v", err) |
| 84 |
} |
| 85 |
|
| 86 |
if resp.StatusCode != http.StatusNoContent { |
| 87 |
t.Errorf("Organizations.RemoveCredentialAuthorization returned %v, want %v", resp.StatusCode, http.StatusNoContent) |
| 88 |
} |
| 89 |
|
| 90 |
const methodName = "RemoveCredentialAuthorization" |
| 91 |
testBadOptions(t, methodName, func() (err error) { |
| 92 |
_, err = client.Organizations.RemoveCredentialAuthorization(ctx, "\n", 0) |
| 93 |
return err |
| 94 |
}) |
| 95 |
|
| 96 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 97 |
return client.Organizations.RemoveCredentialAuthorization(ctx, "o", 1) |
| 98 |
}) |
| 99 |
} |