github.com/google/go-github/v33@v33.0.0/github/authorizations_test.go (about) 1 // Copyright 2015 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 TestAuthorizationsService_Check(t *testing.T) { 17 client, mux, _, teardown := setup() 18 defer teardown() 19 20 mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) { 21 testMethod(t, r, "POST") 22 testBody(t, r, `{"access_token":"a"}`+"\n") 23 testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) 24 fmt.Fprint(w, `{"id":1}`) 25 }) 26 27 got, _, err := client.Authorizations.Check(context.Background(), "id", "a") 28 if err != nil { 29 t.Errorf("Authorizations.Check returned error: %v", err) 30 } 31 32 want := &Authorization{ID: Int64(1)} 33 if !reflect.DeepEqual(got, want) { 34 t.Errorf("Authorizations.Check returned auth %+v, want %+v", got, want) 35 } 36 } 37 38 func TestAuthorizationsService_Reset(t *testing.T) { 39 client, mux, _, teardown := setup() 40 defer teardown() 41 42 mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) { 43 testMethod(t, r, "PATCH") 44 testBody(t, r, `{"access_token":"a"}`+"\n") 45 testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) 46 fmt.Fprint(w, `{"ID":1}`) 47 }) 48 49 got, _, err := client.Authorizations.Reset(context.Background(), "id", "a") 50 if err != nil { 51 t.Errorf("Authorizations.Reset returned error: %v", err) 52 } 53 54 want := &Authorization{ID: Int64(1)} 55 if !reflect.DeepEqual(got, want) { 56 t.Errorf("Authorizations.Reset returned auth %+v, want %+v", got, want) 57 } 58 } 59 60 func TestAuthorizationsService_Revoke(t *testing.T) { 61 client, mux, _, teardown := setup() 62 defer teardown() 63 64 mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) { 65 testMethod(t, r, "DELETE") 66 testBody(t, r, `{"access_token":"a"}`+"\n") 67 testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) 68 w.WriteHeader(http.StatusNoContent) 69 }) 70 71 _, err := client.Authorizations.Revoke(context.Background(), "id", "a") 72 if err != nil { 73 t.Errorf("Authorizations.Revoke returned error: %v", err) 74 } 75 } 76 77 func TestDeleteGrant(t *testing.T) { 78 client, mux, _, teardown := setup() 79 defer teardown() 80 81 mux.HandleFunc("/applications/id/grant", func(w http.ResponseWriter, r *http.Request) { 82 testMethod(t, r, "DELETE") 83 testBody(t, r, `{"access_token":"a"}`+"\n") 84 testHeader(t, r, "Accept", mediaTypeOAuthAppPreview) 85 }) 86 87 _, err := client.Authorizations.DeleteGrant(context.Background(), "id", "a") 88 if err != nil { 89 t.Errorf("OAuthAuthorizations.DeleteGrant returned error: %v", err) 90 } 91 } 92 93 func TestAuthorizationsService_CreateImpersonation(t *testing.T) { 94 client, mux, _, teardown := setup() 95 defer teardown() 96 97 mux.HandleFunc("/admin/users/u/authorizations", func(w http.ResponseWriter, r *http.Request) { 98 testMethod(t, r, "POST") 99 fmt.Fprint(w, `{"id":1}`) 100 }) 101 102 req := &AuthorizationRequest{Scopes: []Scope{ScopePublicRepo}} 103 got, _, err := client.Authorizations.CreateImpersonation(context.Background(), "u", req) 104 if err != nil { 105 t.Errorf("Authorizations.CreateImpersonation returned error: %+v", err) 106 } 107 108 want := &Authorization{ID: Int64(1)} 109 if !reflect.DeepEqual(got, want) { 110 t.Errorf("Authorizations.CreateImpersonation returned %+v, want %+v", *got.ID, *want.ID) 111 } 112 } 113 114 func TestAuthorizationsService_DeleteImpersonation(t *testing.T) { 115 client, mux, _, teardown := setup() 116 defer teardown() 117 118 mux.HandleFunc("/admin/users/u/authorizations", func(w http.ResponseWriter, r *http.Request) { 119 testMethod(t, r, "DELETE") 120 }) 121 122 _, err := client.Authorizations.DeleteImpersonation(context.Background(), "u") 123 if err != nil { 124 t.Errorf("Authorizations.DeleteImpersonation returned error: %+v", err) 125 } 126 }