forked from google/go-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorizations_test.go
More file actions
126 lines (103 loc) · 3.65 KB
/
authorizations_test.go
File metadata and controls
126 lines (103 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestAuthorizationsService_Check(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testBody(t, r, `{"access_token":"a"}`+"\n")
testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
fmt.Fprint(w, `{"id":1}`)
})
got, _, err := client.Authorizations.Check(context.Background(), "id", "a")
if err != nil {
t.Errorf("Authorizations.Check returned error: %v", err)
}
want := &Authorization{ID: Int64(1)}
if !reflect.DeepEqual(got, want) {
t.Errorf("Authorizations.Check returned auth %+v, want %+v", got, want)
}
}
func TestAuthorizationsService_Reset(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
testBody(t, r, `{"access_token":"a"}`+"\n")
testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
fmt.Fprint(w, `{"ID":1}`)
})
got, _, err := client.Authorizations.Reset(context.Background(), "id", "a")
if err != nil {
t.Errorf("Authorizations.Reset returned error: %v", err)
}
want := &Authorization{ID: Int64(1)}
if !reflect.DeepEqual(got, want) {
t.Errorf("Authorizations.Reset returned auth %+v, want %+v", got, want)
}
}
func TestAuthorizationsService_Revoke(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/applications/id/token", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testBody(t, r, `{"access_token":"a"}`+"\n")
testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
w.WriteHeader(http.StatusNoContent)
})
_, err := client.Authorizations.Revoke(context.Background(), "id", "a")
if err != nil {
t.Errorf("Authorizations.Revoke returned error: %v", err)
}
}
func TestDeleteGrant(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/applications/id/grant", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testBody(t, r, `{"access_token":"a"}`+"\n")
testHeader(t, r, "Accept", mediaTypeOAuthAppPreview)
})
_, err := client.Authorizations.DeleteGrant(context.Background(), "id", "a")
if err != nil {
t.Errorf("OAuthAuthorizations.DeleteGrant returned error: %v", err)
}
}
func TestAuthorizationsService_CreateImpersonation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/admin/users/u/authorizations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1}`)
})
req := &AuthorizationRequest{Scopes: []Scope{ScopePublicRepo}}
got, _, err := client.Authorizations.CreateImpersonation(context.Background(), "u", req)
if err != nil {
t.Errorf("Authorizations.CreateImpersonation returned error: %+v", err)
}
want := &Authorization{ID: Int64(1)}
if !reflect.DeepEqual(got, want) {
t.Errorf("Authorizations.CreateImpersonation returned %+v, want %+v", *got.ID, *want.ID)
}
}
func TestAuthorizationsService_DeleteImpersonation(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/admin/users/u/authorizations", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
_, err := client.Authorizations.DeleteImpersonation(context.Background(), "u")
if err != nil {
t.Errorf("Authorizations.DeleteImpersonation returned error: %+v", err)
}
}