|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + jwt "github.com/golang-jwt/jwt/v5" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + snapchatUser = `{"data":{"me":{"externalId":"snapchatTestId","displayName":"Snapchat Test","bitmoji":{"avatar":"http://example.com/bitmoji"}}}}` |
| 14 | +) |
| 15 | + |
| 16 | +func (ts *ExternalTestSuite) TestSignupExternalSnapchat() { |
| 17 | + req := httptest.NewRequest(http.MethodGet, "http://localhost/authorize?provider=snapchat", nil) |
| 18 | + w := httptest.NewRecorder() |
| 19 | + ts.API.handler.ServeHTTP(w, req) |
| 20 | + ts.Require().Equal(http.StatusFound, w.Code) |
| 21 | + u, err := url.Parse(w.Header().Get("Location")) |
| 22 | + ts.Require().NoError(err, "redirect url parse failed") |
| 23 | + q := u.Query() |
| 24 | + ts.Equal(ts.Config.External.Snapchat.RedirectURI, q.Get("redirect_uri")) |
| 25 | + ts.Equal(ts.Config.External.Snapchat.ClientID, []string{q.Get("client_id")}) |
| 26 | + ts.Equal("code", q.Get("response_type")) |
| 27 | + ts.Equal("https://auth.snapchat.com/oauth2/api/user.external_id https://auth.snapchat.com/oauth2/api/user.display_name https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar", q.Get("scope")) |
| 28 | + |
| 29 | + claims := ExternalProviderClaims{} |
| 30 | + p := jwt.NewParser(jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Name})) |
| 31 | + _, err = p.ParseWithClaims(q.Get("state"), &claims, func(token *jwt.Token) (interface{}, error) { |
| 32 | + return []byte(ts.Config.JWT.Secret), nil |
| 33 | + }) |
| 34 | + ts.Require().NoError(err) |
| 35 | + |
| 36 | + ts.Equal("snapchat", claims.Provider) |
| 37 | + ts.Equal(ts.Config.SiteURL, claims.SiteURL) |
| 38 | +} |
| 39 | + |
| 40 | +func SnapchatTestSignupSetup(ts *ExternalTestSuite, tokenCount *int, userCount *int, code string, user string) *httptest.Server { |
| 41 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 42 | + switch r.URL.Path { |
| 43 | + case "/accounts/oauth2/token": |
| 44 | + *tokenCount++ |
| 45 | + ts.Equal(code, r.FormValue("code")) |
| 46 | + ts.Equal("authorization_code", r.FormValue("grant_type")) |
| 47 | + ts.Equal(ts.Config.External.Snapchat.RedirectURI, r.FormValue("redirect_uri")) |
| 48 | + |
| 49 | + w.Header().Add("Content-Type", "application/json") |
| 50 | + fmt.Fprint(w, `{"access_token":"snapchat_token","expires_in":3600}`) |
| 51 | + case "/v1/me": |
| 52 | + *userCount++ |
| 53 | + w.Header().Add("Content-Type", "application/json") |
| 54 | + fmt.Fprint(w, user) |
| 55 | + default: |
| 56 | + w.WriteHeader(500) |
| 57 | + ts.Fail("unknown snapchat oauth call %s", r.URL.Path) |
| 58 | + } |
| 59 | + })) |
| 60 | + |
| 61 | + ts.Config.External.Snapchat.URL = server.URL |
| 62 | + |
| 63 | + return server |
| 64 | +} |
| 65 | + |
| 66 | +func (ts *ExternalTestSuite) TestSignupExternalSnapchat_AuthorizationCode() { |
| 67 | + ts.Config.DisableSignup = false |
| 68 | + tokenCount, userCount := 0, 0 |
| 69 | + code := "authcode" |
| 70 | + server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser) |
| 71 | + defer server.Close() |
| 72 | + |
| 73 | + u := performAuthorization(ts, "snapchat", code, "") |
| 74 | + |
| 75 | + assertAuthorizationSuccess( ts, u, tokenCount, userCount, "[email protected]", "Snapchat Test", "snapchatTestId", "http://example.com/bitmoji") |
| 76 | +} |
| 77 | + |
| 78 | +func (ts *ExternalTestSuite) TestSignupExternalSnapchatDisableSignupErrorWhenNoUser() { |
| 79 | + ts.Config.DisableSignup = true |
| 80 | + |
| 81 | + tokenCount, userCount := 0, 0 |
| 82 | + code := "authcode" |
| 83 | + server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser) |
| 84 | + defer server.Close() |
| 85 | + |
| 86 | + u := performAuthorization(ts, "snapchat", code, "") |
| 87 | + |
| 88 | + assertAuthorizationFailure(ts, u, "Signups not allowed for this instance", "access_denied", "") |
| 89 | +} |
| 90 | + |
| 91 | +func (ts *ExternalTestSuite) TestSignupExternalSnapchatDisableSignupSuccessWithExistingUser() { |
| 92 | + ts.Config.DisableSignup = true |
| 93 | + |
| 94 | + ts. createUser( "snapchatTestId", "[email protected]", "Snapchat Test", "http://example.com/bitmoji", "") |
| 95 | + |
| 96 | + tokenCount, userCount := 0, 0 |
| 97 | + code := "authcode" |
| 98 | + server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser) |
| 99 | + defer server.Close() |
| 100 | + |
| 101 | + u := performAuthorization(ts, "snapchat", code, "") |
| 102 | + |
| 103 | + assertAuthorizationSuccess( ts, u, tokenCount, userCount, "[email protected]", "Snapchat Test", "snapchatTestId", "http://example.com/bitmoji") |
| 104 | +} |
| 105 | + |
| 106 | +func (ts *ExternalTestSuite) TestInviteTokenExternalSnapchatSuccessWhenMatchingToken() { |
| 107 | + // name and avatar should be populated from Snapchat API |
| 108 | + // Use the same email that the provider will generate - converted to lowercase |
| 109 | + ts. createUser( "snapchatTestId", "[email protected]", "", "", "invite_token") |
| 110 | + |
| 111 | + tokenCount, userCount := 0, 0 |
| 112 | + code := "authcode" |
| 113 | + server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser) |
| 114 | + defer server.Close() |
| 115 | + |
| 116 | + u := performAuthorization(ts, "snapchat", code, "invite_token") |
| 117 | + |
| 118 | + assertAuthorizationSuccess( ts, u, tokenCount, userCount, "[email protected]", "Snapchat Test", "snapchatTestId", "http://example.com/bitmoji") |
| 119 | +} |
| 120 | + |
| 121 | +func (ts *ExternalTestSuite) TestInviteTokenExternalSnapchatErrorWhenNoMatchingToken() { |
| 122 | + tokenCount, userCount := 0, 0 |
| 123 | + code := "authcode" |
| 124 | + server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser) |
| 125 | + defer server.Close() |
| 126 | + |
| 127 | + w := performAuthorizationRequest(ts, "snapchat", "invite_token") |
| 128 | + ts.Require().Equal(http.StatusNotFound, w.Code) |
| 129 | +} |
| 130 | + |
| 131 | +func (ts *ExternalTestSuite) TestInviteTokenExternalSnapchatErrorWhenWrongToken() { |
| 132 | + ts.createUser("snapchatTestId", "", "", "", "invite_token") |
| 133 | + |
| 134 | + tokenCount, userCount := 0, 0 |
| 135 | + code := "authcode" |
| 136 | + server := SnapchatTestSignupSetup(ts, &tokenCount, &userCount, code, snapchatUser) |
| 137 | + defer server.Close() |
| 138 | + |
| 139 | + w := performAuthorizationRequest(ts, "snapchat", "wrong_token") |
| 140 | + ts.Require().Equal(http.StatusNotFound, w.Code) |
| 141 | +} |
0 commit comments