Debian Codegraph @

scrape/apps_test.go

raw
1 package scrape
2
3 import (
4 "net/http"
5 "testing"
6
7 "github.com/google/go-cmp/cmp"
8 "github.com/google/go-github/v59/github"
9 )
10
11 func Test_AppRestrictionsEnabled(t *testing.T) {
12 tests := []struct {
13 description string
14 testFile string
15 org string
16 want bool
17 }{
18 {
19 description: "return true for enabled orgs",
20 testFile: "access-restrictions-enabled.html",
21 want: true,
22 },
23 {
24 description: "return false for disabled orgs",
25 testFile: "access-restrictions-disabled.html",
26 want: false,
27 },
28 }
29
30 for _, tt := range tests {
31 t.Run(tt.description, func(t *testing.T) {
32 client, mux, cleanup := setup()
33 defer cleanup()
34
35 mux.HandleFunc("/organizations/o/settings/oauth_application_policy", func(w http.ResponseWriter, r *http.Request) {
36 copyTestFile(t, w, tt.testFile)
37 })
38
39 got, err := client.AppRestrictionsEnabled("o")
40 if err != nil {
41 t.Errorf("AppRestrictionsEnabled returned err: %v", err)
42 }
43 if want := tt.want; got != want {
44 t.Errorf("AppRestrictionsEnabled returned %t, want %t", got, want)
45 }
46 })
47 }
48 }
49
50 func Test_ListOAuthApps(t *testing.T) {
51 client, mux, cleanup := setup()
52 defer cleanup()
53
54 mux.HandleFunc("/organizations/e/settings/oauth_application_policy", func(w http.ResponseWriter, r *http.Request) {
55 copyTestFile(t, w, "access-restrictions-enabled.html")
56 })
57
58 got, err := client.ListOAuthApps("e")
59 if err != nil {
60 t.Errorf("ListOAuthApps(e) returned err: %v", err)
61 }
62 want := []OAuthApp{
63 {
64 ID: 22222,
65 Name: "Coveralls",
66 Description: "Test coverage history and statistics.",
67 State: OAuthAppRequested,
68 RequestedBy: "willnorris",
69 },
70 {
71 ID: 530107,
72 Name: "Google Cloud Platform",
73 State: OAuthAppApproved,
74 },
75 {
76 ID: 231424,
77 Name: "GitKraken",
78 Description: "An intuitive, cross-platform Git client that doesn't suck, built by @axosoft and made with @nodegit & @ElectronJS.",
79 State: OAuthAppDenied,
80 },
81 }
82 if !cmp.Equal(got, want) {
83 t.Errorf("ListOAuthApps(o) returned %v, want %v", got, want)
84 }
85 }
86
87 func Test_CreateApp(t *testing.T) {
88 client, mux, cleanup := setup()
89 defer cleanup()
90
91 mux.HandleFunc("/apps/settings/new", func(w http.ResponseWriter, r *http.Request) {
92 w.WriteHeader(http.StatusCreated)
93 })
94
95 if _, err := client.CreateApp(&AppManifest{
96 URL: github.String("https://example.com"),
97 HookAttributes: map[string]string{
98 "url": "https://example.com/hook",
99 },
100 }); err != nil {
101 t.Fatalf("CreateApp: %v", err)
102 }
103 }