-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
276 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
internal/mock_api/authentication/authentication_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package authentication | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/twitchdev/twitch-cli/internal/database" | ||
"github.com/twitchdev/twitch-cli/internal/util" | ||
"github.com/twitchdev/twitch-cli/test_setup" | ||
) | ||
|
||
var a *assert.Assertions | ||
var ac = database.AuthenticationClient{ID: "1234", Secret: "1234", Name: "test_client", IsExtension: false} | ||
var token = "potato" | ||
var firstRun = true | ||
|
||
func TestHasScope(t *testing.T) { | ||
a = test_setup.SetupTestEnv(t) | ||
|
||
u := UserAuthentication{UserID: "1", Scopes: []string{"user:read:email"}} | ||
|
||
a.Equal(true, u.HasScope("user:read:email")) | ||
a.Equal(false, u.HasScope("user:read")) | ||
|
||
a.Equal(true, u.HasOneOfRequiredScope([]string{})) | ||
a.Equal(true, u.HasOneOfRequiredScope([]string{"user:read:email", "user:read"})) | ||
a.Equal(false, u.HasOneOfRequiredScope([]string{"user:read"})) | ||
|
||
} | ||
|
||
func TestNatchesBroadcasterIDParam(t *testing.T) { | ||
a = test_setup.SetupTestEnv(t) | ||
|
||
req, _ := http.NewRequest(http.MethodGet, "http://google.com", nil) | ||
u := UserAuthentication{UserID: "1", Scopes: []string{"user:read:email"}} | ||
|
||
q := req.URL.Query() | ||
q.Set("broadcaster_id", "2") | ||
req.URL.RawQuery = q.Encode() | ||
|
||
a.Equal(false, u.MatchesBroadcasterIDParam(req)) | ||
|
||
q.Set("broadcaster_id", "1") | ||
req.URL.RawQuery = q.Encode() | ||
|
||
a.Equal(true, u.MatchesBroadcasterIDParam(req)) | ||
} | ||
|
||
func TestAuthenticationMiddleware(t *testing.T) { | ||
a = test_setup.SetupTestEnv(t) | ||
ts := httptest.NewServer(baseMiddleware(AuthenticationMiddleware(testEndpoint{}))) | ||
|
||
req, _ := http.NewRequest(http.MethodGet, ts.URL+testEndpoint{}.Path(), nil) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(401, resp.StatusCode) | ||
|
||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(401, resp.StatusCode) | ||
|
||
req.Header.Set("Client-ID", ac.ID) | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(401, resp.StatusCode) | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(200, resp.StatusCode) | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer%v", token)) | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(401, resp.StatusCode) | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("Bearer")) | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(401, resp.StatusCode) | ||
} | ||
|
||
func baseMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.Background() | ||
|
||
// just stub it all | ||
db, err := database.NewConnection() | ||
if err != nil { | ||
log.Fatalf("Error connecting to database: %v", err.Error()) | ||
return | ||
} | ||
if firstRun == true { | ||
ac, err = db.NewQuery(r, 100).InsertOrUpdateAuthenticationClient(ac, false) | ||
a.Nil(err) | ||
auth, err := db.NewQuery(r, 100).CreateAuthorization(database.Authorization{ClientID: ac.ID, UserID: "1", Scopes: "user:read:email bits:read", Token: token, ExpiresAt: util.GetTimestamp().Add(7 * 24 * time.Hour).Format(time.RFC3339)}) | ||
token = auth.Token | ||
a.Nil(err) | ||
|
||
firstRun = false | ||
} | ||
|
||
defer db.DB.Close() | ||
|
||
ctx = context.WithValue(ctx, "db", db) | ||
r = r.WithContext(ctx) | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
type testEndpoint struct{} | ||
|
||
func (e testEndpoint) Path() string { return "/endpoint" } | ||
|
||
func (e testEndpoint) GetRequiredScopes(method string) []string { | ||
return []string{} | ||
} | ||
|
||
func (e testEndpoint) ValidMethod(method string) bool { | ||
return true | ||
} | ||
|
||
func (e testEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
userCtx := r.Context().Value("auth").(UserAuthentication) | ||
|
||
a.NotNil(userCtx) | ||
w.WriteHeader(200) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package mock_auth | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/twitchdev/twitch-cli/internal/database" | ||
"github.com/twitchdev/twitch-cli/internal/util" | ||
"github.com/twitchdev/twitch-cli/test_setup" | ||
) | ||
|
||
var a *assert.Assertions | ||
var firstRun = true | ||
var ac = database.AuthenticationClient{ID: "222", Secret: "333", Name: "test_client", IsExtension: false} | ||
|
||
func TestAreValidScopes(t *testing.T) { | ||
a := test_setup.SetupTestEnv(t) | ||
|
||
a.Equal(true, areValidScopes([]string{"user:read:email"}, USER_ACCESS_TOKEN)) | ||
a.Equal(false, areValidScopes([]string{"user:read:email"}, APP_ACCES_TOKEN)) | ||
} | ||
|
||
func TestUserToken(t *testing.T) { | ||
a = test_setup.SetupTestEnv(t) | ||
ts := httptest.NewServer(baseMiddleware(UserTokenEndpoint{})) | ||
|
||
req, _ := http.NewRequest(http.MethodPost, ts.URL+UserTokenEndpoint{}.Path(), nil) | ||
q := req.URL.Query() | ||
|
||
req.URL.RawQuery = q.Encode() | ||
resp, err := http.DefaultClient.Do(req) | ||
a.Nil(err, err) | ||
a.Equal(400, resp.StatusCode) | ||
|
||
// valid values | ||
q.Set("client_id", ac.ID) | ||
q.Set("client_secret", ac.Secret) | ||
q.Set("grant_type", "user_token") | ||
q.Set("user_id", "1") | ||
|
||
q.Set("scope", "potato") | ||
req.URL.RawQuery = q.Encode() | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(400, resp.StatusCode) | ||
|
||
q.Set("scope", "") | ||
req.URL.RawQuery = q.Encode() | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(200, resp.StatusCode) | ||
|
||
q.Set("client_id", "1234") | ||
req.URL.RawQuery = q.Encode() | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(400, resp.StatusCode) | ||
|
||
q.Set("client_id", ac.ID) | ||
q.Set("user_id", util.RandomGUID()) | ||
req.URL.RawQuery = q.Encode() | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(400, resp.StatusCode) | ||
} | ||
|
||
func TestAppAccessToken(t *testing.T) { | ||
a = test_setup.SetupTestEnv(t) | ||
ts := httptest.NewServer(baseMiddleware(AppAccessTokenEndpoint{})) | ||
|
||
req, _ := http.NewRequest(http.MethodPost, ts.URL+AppAccessTokenEndpoint{}.Path(), nil) | ||
q := req.URL.Query() | ||
|
||
req.URL.RawQuery = q.Encode() | ||
resp, err := http.DefaultClient.Do(req) | ||
a.Nil(err, err) | ||
a.Equal(400, resp.StatusCode) | ||
|
||
// valid values | ||
q.Set("client_id", ac.ID) | ||
q.Set("client_secret", ac.Secret) | ||
q.Set("grant_type", "client_credentials") | ||
|
||
q.Set("scope", "potato") | ||
req.URL.RawQuery = q.Encode() | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(400, resp.StatusCode) | ||
|
||
q.Set("scope", "") | ||
req.URL.RawQuery = q.Encode() | ||
resp, err = http.DefaultClient.Do(req) | ||
a.Nil(err) | ||
a.Equal(200, resp.StatusCode) | ||
} | ||
func baseMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := context.Background() | ||
|
||
// just stub it all | ||
db, err := database.NewConnection() | ||
if err != nil { | ||
log.Fatalf("Error connecting to database: %v", err.Error()) | ||
return | ||
} | ||
if firstRun == true { | ||
ac, err = db.NewQuery(r, 100).InsertOrUpdateAuthenticationClient(ac, false) | ||
a.Nil(err, err) | ||
|
||
firstRun = false | ||
} | ||
|
||
defer db.DB.Close() | ||
|
||
ctx = context.WithValue(ctx, "db", db) | ||
r = r.WithContext(ctx) | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters