-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix recording of tests when using a separate authentication api instance #322
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4dcdda6
Fix recording of tests when using a separate authentication api instance
ewanharris 2dd1979
Update instructions for running tests
ewanharris fb1f544
Improve coverage of auth client when running E2E tests
ewanharris c3a9a9a
Check error when deleting user
ewanharris 5334739
Fix review comments
ewanharris fe04df7
Merge branch 'main' into test/SDK-4359-auth-e2e
ewanharris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -24,15 +24,19 @@ import ( | |
"github.com/auth0/go-auth0/authentication/database" | ||
"github.com/auth0/go-auth0/authentication/oauth" | ||
"github.com/auth0/go-auth0/internal/client" | ||
"github.com/auth0/go-auth0/management" | ||
) | ||
|
||
var ( | ||
domain = os.Getenv("AUTH0_DOMAIN") | ||
clientID = os.Getenv("AUTH0_AUTH_CLIENT_ID") | ||
clientSecret = os.Getenv("AUTH0_AUTH_CLIENT_SECRET") | ||
mgmtClientID = os.Getenv("AUTH0_CLIENT_ID") | ||
mgmtClientSecret = os.Getenv("AUTH0_CLIENT_SECRET") | ||
httpRecordings = os.Getenv("AUTH0_HTTP_RECORDINGS") | ||
httpRecordingsEnabled = false | ||
authAPI = &Authentication{} | ||
mgmtAPI = &management.Management{} | ||
jwtPublicKey = `-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8foXPIpkeLKAVfVg/W0X | ||
steFas2XwrxAGG0lnLS3mc/cYc/pD/plsR779O8It/2YmHFWIDmCIcW57boDae/K | ||
|
@@ -99,11 +103,20 @@ func initializeTestClient() { | |
context.Background(), | ||
domain, | ||
WithClientID(clientID), | ||
WithIDTokenSigningAlg("HS256"), | ||
// WithIDTokenSigningAlg("HS256"), | ||
) | ||
if err != nil { | ||
log.Fatal("failed to initialize the auth api client") | ||
} | ||
|
||
mgmtAPI, err = management.New( | ||
domain, | ||
management.WithClientCredentials(context.Background(), mgmtClientID, mgmtClientSecret), | ||
) | ||
|
||
if err != nil { | ||
log.Fatal("failed to initialize the management api client") | ||
} | ||
} | ||
|
||
func TestAuthenticationNew(t *testing.T) { | ||
|
@@ -188,7 +201,8 @@ func TestAuthenticationApiCallContextTimeout(t *testing.T) { | |
} | ||
|
||
func TestUserInfo(t *testing.T) { | ||
configureHTTPTestRecordings(t) | ||
skipE2E(t) | ||
configureHTTPTestRecordings(t, authAPI) | ||
|
||
user, err := authAPI.UserInfo(context.Background(), "test-access-token") | ||
|
||
|
@@ -487,3 +501,47 @@ func TestWithClockTolerance(t *testing.T) { | |
}, oauth.IDTokenValidationOptions{}) | ||
assert.ErrorContains(t, err, "\"iat\" not satisfied") | ||
} | ||
|
||
func skipE2E(t *testing.T) { | ||
t.Helper() | ||
|
||
if !httpRecordingsEnabled { | ||
t.Skip("Skipped as cannot be test in E2E scenario") | ||
} | ||
} | ||
|
||
func usingRecordingResponses(t *testing.T) bool { | ||
t.Helper() | ||
|
||
return httpRecordingsEnabled && domain == "go-auth0-dev.eu.auth0.com" | ||
} | ||
|
||
func givenAUser(t *testing.T) userDetails { | ||
t.Helper() | ||
|
||
if !usingRecordingResponses(t) { | ||
user := &management.User{ | ||
Connection: auth0.String("Username-Password-Authentication"), | ||
Email: auth0.String("[email protected]"), | ||
Password: auth0.String("Testpassword123!"), | ||
Username: auth0.String("test-user"), | ||
EmailVerified: auth0.Bool(true), | ||
VerifyEmail: auth0.Bool(false), | ||
} | ||
|
||
err := mgmtAPI.User.Create(context.Background(), user) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
err := mgmtAPI.User.Delete(context.Background(), user.GetID()) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
return userDetails{ | ||
connection: "Username-Password-Authentication", | ||
email: "[email protected]", | ||
password: "Testpassword123!", | ||
username: "test-user", | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,31 +2,39 @@ package authentication | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/auth0/go-auth0" | ||
"github.com/auth0/go-auth0/authentication/database" | ||
"github.com/auth0/go-auth0/management" | ||
) | ||
|
||
func TestDatabaseSignUp(t *testing.T) { | ||
configureHTTPTestRecordings(t) | ||
configureHTTPTestRecordings(t, authAPI) | ||
|
||
details := givenSignUpDetails(t) | ||
|
||
userData := database.SignupRequest{ | ||
Connection: "Username-Password-Authentication", | ||
Username: "mytestaccount", | ||
Password: "mypassword", | ||
Email: "[email protected]", | ||
Connection: details.connection, | ||
Username: details.username, | ||
Password: details.password, | ||
Email: details.email, | ||
} | ||
|
||
createdUser, err := authAPI.Database.Signup(context.Background(), userData) | ||
assert.NoError(t, err) | ||
require.NoError(t, err) | ||
assert.NotEmpty(t, createdUser.ID) | ||
assert.Equal(t, userData.Username, createdUser.Username) | ||
} | ||
|
||
func TestDatabaseChangePassword(t *testing.T) { | ||
configureHTTPTestRecordings(t) | ||
configureHTTPTestRecordings(t, authAPI) | ||
|
||
resp, err := authAPI.Database.ChangePassword(context.Background(), database.ChangePasswordRequest{ | ||
Connection: "Username-Password-Authentication", | ||
|
@@ -36,3 +44,53 @@ func TestDatabaseChangePassword(t *testing.T) { | |
assert.NoError(t, err) | ||
assert.Equal(t, "We've just sent you an email to reset your password.", resp) | ||
} | ||
|
||
type userDetails struct { | ||
username string | ||
password string | ||
email string | ||
connection string | ||
} | ||
|
||
func givenSignUpDetails(t *testing.T) *userDetails { | ||
t.Helper() | ||
// If we're running from recordings then we want to return the default | ||
if usingRecordingResponses(t) { | ||
return &userDetails{ | ||
username: "mytestaccount", | ||
password: "mypassword", | ||
email: "[email protected]", | ||
connection: "Username-Password-Authentication", | ||
} | ||
} | ||
|
||
conn := givenAConnection(t) | ||
|
||
return &userDetails{ | ||
username: fmt.Sprintf("chuck%d", rand.Intn(999)), | ||
password: "Passwords hide their chuck", | ||
email: fmt.Sprintf("chuck%[email protected]", rand.Intn(999)), | ||
connection: conn.GetName(), | ||
} | ||
} | ||
|
||
func givenAConnection(t *testing.T) management.Connection { | ||
conn := &management.Connection{ | ||
Name: auth0.Stringf("Test-Connection-%d", time.Now().Unix()), | ||
Strategy: auth0.String("auth0"), | ||
EnabledClients: &[]string{clientID, mgmtClientID}, | ||
Options: &management.ConnectionOptions{ | ||
RequiresUsername: auth0.Bool(true), | ||
}, | ||
} | ||
|
||
err := mgmtAPI.Connection.Create(context.Background(), conn) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
err := mgmtAPI.Connection.Delete(context.Background(), conn.GetID()) | ||
require.NoError(t, err) | ||
}) | ||
|
||
return *conn | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be
AUTH0_MGMT_CLIENT_ID
andAUTH0_MGMT_CLIENT_SECRET
to more clearly distinguish them from their auth counterparts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd left these as matching what's used for the management tests to make it easier to reuse them (some folks might opt to set environment variables themselves instead of the .env file)