onasty/internal/oauth/github_test.go (view raw)
| 1 | package oauth |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/require" |
| 14 | "golang.org/x/oauth2" |
| 15 | ) |
| 16 | |
| 17 | func TestGitHubProvider_GetAuthURL(t *testing.T) { |
| 18 | provider := NewGithubProvider("client.id", "secret", "http://localhost/callback") |
| 19 | url := provider.GetAuthURL("test") |
| 20 | |
| 21 | assert.Contains(t, url, "client_id=client.id") |
| 22 | assert.Contains(t, url, "state=test") |
| 23 | assert.Contains(t, url, "scope=user%3Aemail") |
| 24 | } |
| 25 | |
| 26 | type mockClient func(*http.Request) (*http.Response, error) |
| 27 | |
| 28 | func (m mockClient) RoundTrip(req *http.Request) (*http.Response, error) { |
| 29 | return m(req) |
| 30 | } |
| 31 | |
| 32 | func TestGitHubProvider_ExchangeCode(t *testing.T) { |
| 33 | userID := "123123" |
| 34 | userEmail := "test@testing.org" |
| 35 | userLogin := "testing" |
| 36 | |
| 37 | resp := fmt.Sprintf(`{"id":%s, "email":"%s", "login":"%s"}`, userID, userEmail, userLogin) |
| 38 | client := &http.Client{ |
| 39 | Transport: mockClient(func(req *http.Request) (*http.Response, error) { |
| 40 | if req.Method == http.MethodPost { |
| 41 | return &http.Response{ |
| 42 | StatusCode: http.StatusOK, |
| 43 | Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 44 | Body: io.NopCloser( |
| 45 | strings.NewReader(`{"access_token":"fake", |
| 46 | "token_type":"bearer", |
| 47 | "expires_in":3600}`), |
| 48 | ), |
| 49 | }, nil |
| 50 | } |
| 51 | return &http.Response{ |
| 52 | StatusCode: http.StatusOK, |
| 53 | Header: http.Header{"Content-Type": []string{"application/json"}}, |
| 54 | Body: io.NopCloser(strings.NewReader(resp)), |
| 55 | }, nil |
| 56 | }), |
| 57 | } |
| 58 | |
| 59 | provider := NewGithubProvider("client.id", "secret", "http://localhost") |
| 60 | ctx := context.WithValue(context.TODO(), oauth2.HTTPClient, client) |
| 61 | |
| 62 | info, err := provider.ExchangeCode(ctx, "") |
| 63 | require.NoError(t, err) |
| 64 | assert.Equal(t, "github", info.Provider) |
| 65 | assert.Equal(t, userID, info.ProviderID) |
| 66 | assert.Equal(t, userEmail, info.Email) |
| 67 | } |
| 68 | |
| 69 | func TestGitHubProvider_ExchangeCode_tokenExcahnge_error(t *testing.T) { |
| 70 | client := &http.Client{ |
| 71 | Transport: mockClient(func(req *http.Request) (*http.Response, error) { |
| 72 | if req.Method == http.MethodPost { |
| 73 | return &http.Response{ |
| 74 | StatusCode: http.StatusBadRequest, |
| 75 | Body: io.NopCloser(strings.NewReader("")), |
| 76 | }, nil |
| 77 | } |
| 78 | return nil, errors.New("unexpected request") |
| 79 | }), |
| 80 | } |
| 81 | |
| 82 | provider := NewGithubProvider("client.id", "secret", "http://localhost") |
| 83 | ctx := context.WithValue(context.TODO(), oauth2.HTTPClient, client) |
| 84 | |
| 85 | _, err := provider.ExchangeCode(ctx, "") |
| 86 | require.Error(t, err) |
| 87 | } |