| 11 | ) |
| 12 | |
| 13 | func Test_HasMinimumScopes(t *testing.T) { |
| 14 | tests := []struct { |
| 15 | name string |
| 16 | header string |
| 17 | wantErr string |
| 18 | }{ |
| 19 | { |
| 20 | name: "write:org satisfies read:org", |
| 21 | header: "repo, write:org", |
| 22 | wantErr: "", |
| 23 | }, |
| 24 | { |
| 25 | name: "insufficient scope", |
| 26 | header: "repo", |
| 27 | wantErr: "missing required scope 'read:org'", |
| 28 | }, |
| 29 | } |
| 30 | for _, tt := range tests { |
| 31 | t.Run(tt.name, func(t *testing.T) { |
| 32 | fakehttp := &httpmock.Registry{} |
| 33 | defer fakehttp.Verify(t) |
| 34 | |
| 35 | var gotAuthorization string |
| 36 | fakehttp.Register(httpmock.REST("GET", ""), func(req *http.Request) (*http.Response, error) { |
| 37 | gotAuthorization = req.Header.Get("authorization") |
| 38 | return &http.Response{ |
| 39 | Request: req, |
| 40 | StatusCode: 200, |
| 41 | Body: io.NopCloser(&bytes.Buffer{}), |
| 42 | Header: map[string][]string{ |
| 43 | "X-Oauth-Scopes": {tt.header}, |
| 44 | }, |
| 45 | }, nil |
| 46 | }) |
| 47 | |
| 48 | client := http.Client{Transport: fakehttp} |
| 49 | err := HasMinimumScopes(&client, "github.com", "ATOKEN") |
| 50 | if tt.wantErr != "" { |
| 51 | assert.EqualError(t, err, tt.wantErr) |
| 52 | } else { |
| 53 | assert.NoError(t, err) |
| 54 | } |
| 55 | assert.Equal(t, gotAuthorization, "token ATOKEN") |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func Test_HeaderHasMinimumScopes(t *testing.T) { |
| 61 | tests := []struct { |