Skip to content

Commit

Permalink
Merge branch 'main' into SDK-4865-HRI-support-in-go-auth0
Browse files Browse the repository at this point in the history
  • Loading branch information
developerkunal committed Aug 6, 2024
2 parents 9f2a15b + 234fb62 commit e02f2a8
Show file tree
Hide file tree
Showing 25 changed files with 1,874 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @auth0/dx-sdks-engineer
* @auth0/project-dx-sdks-engineer-codeowner
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: go build ./...

- name: Check for linting errors
uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 # pin@6.0.1
uses: golangci/golangci-lint-action@aaa42aa0628b4ae2578232a66b541047968fac86 # pin@6.1.0
with:
version: latest
args: -v -c .golangci.yml
Expand Down
4 changes: 4 additions & 0 deletions authentication/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type SignupRequest struct {
Connection string `json:"connection,omitempty"`
// The user's username. Only valid if the connection requires a username.
Username string `json:"username,omitempty"`
// The user's phone number.
PhoneNumber string `json:"phone_number,omitempty"`
// The user's given name(s).
GivenName string `json:"given_name,omitempty"`
// The user's family name(s).
Expand All @@ -43,6 +45,8 @@ type SignupResponse struct {
EmailVerified bool `json:"email_verified,omitempty"`
// The user's ID.
ID string `json:"_id,omitempty"`
// The user's phone number.
PhoneNumber string `json:"phone_number,omitempty"`
// The user's username. Only valid if the connection requires a username.
Username string `json:"username,omitempty"`
// The user's given name(s).
Expand Down
312 changes: 290 additions & 22 deletions authentication/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,135 @@ import (
"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) {
// TestDatabaseSignUp_RequiresUsername tests the Database.Signup method with a connection that requires a username.
func TestDatabaseSignUp_RequiresUsername(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
RequiresUsername: auth0.Bool(true),
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Username: details.username,
Password: details.password,
Email: details.email,
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Username, createdUser.Username)
}

// TestDatabaseSignUp_WithEmailIdentifier tests the Database.Signup method with a connection that requires an email.
func TestDatabaseSignUp_WithEmailIdentifier(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
Attributes: &management.ConnectionOptionsAttributes{
Email: &management.ConnectionOptionsEmailAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
Verification: &management.ConnectionOptionsAttributeVerification{
Active: auth0.Bool(false),
},
},
},
},
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Password: details.password,
Email: details.email,
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Email, createdUser.Email)
}

// TestDatabaseSignUp_WithUsernameIdentifier tests the Database.Signup method with a connection that requires a username.
func TestDatabaseSignUp_WithUsernameIdentifier(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
Attributes: &management.ConnectionOptionsAttributes{
Username: &management.ConnectionOptionsUsernameAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
},
},
},
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Password: details.password,
Username: details.username,
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Username, createdUser.Username)
}

// TestDatabaseSignUp_WithUsernameAndEmailIdentifiers tests the Database.Signup method with a connection that requires both a username and email.
func TestDatabaseSignUp_WithUsernameAndEmailIdentifiers(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
Attributes: &management.ConnectionOptionsAttributes{
Username: &management.ConnectionOptionsUsernameAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
},
},
Email: &management.ConnectionOptionsEmailAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
Verification: &management.ConnectionOptionsAttributeVerification{
Active: auth0.Bool(false),
},
},
},
},
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t)
details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Expand All @@ -29,10 +150,157 @@ func TestDatabaseSignUp(t *testing.T) {

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Username, createdUser.Username)
assert.Equal(t, userData.Email, createdUser.Email)
}

// TestDatabaseSignUp_WithPhoneNumberIdentifier tests the Database.Signup method with a connection that requires a phone number.
func TestDatabaseSignUp_WithPhoneNumberIdentifier(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
Attributes: &management.ConnectionOptionsAttributes{
PhoneNumber: &management.ConnectionOptionsPhoneNumberAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
Verification: &management.ConnectionOptionsAttributeVerification{
Active: auth0.Bool(false),
},
},
},
},
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Password: details.password,
PhoneNumber: details.phoneNumber,
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.PhoneNumber, createdUser.PhoneNumber)
}

// TestDatabaseSignUp_WithUsernameAndPhoneNumberIdentifiers tests the Database.Signup method with a connection that requires both a username and phone number.
func TestDatabaseSignUp_WithUsernameAndPhoneNumberIdentifiers(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
Attributes: &management.ConnectionOptionsAttributes{
Username: &management.ConnectionOptionsUsernameAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
},
},
PhoneNumber: &management.ConnectionOptionsPhoneNumberAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
Verification: &management.ConnectionOptionsAttributeVerification{
Active: auth0.Bool(false),
},
},
},
},
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Username: details.username,
Password: details.password,
PhoneNumber: details.phoneNumber,
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Username, createdUser.Username)
assert.Equal(t, userData.PhoneNumber, createdUser.PhoneNumber)
}

// TestDatabaseSignUp_WithUsernameEmailAndPhoneNumberIdentifiers tests the Database.Signup method with a connection that requires a username, email, and phone number.
func TestDatabaseSignUp_WithUsernameEmailAndPhoneNumberIdentifiers(t *testing.T) {
connectionOptions := &management.ConnectionOptions{
Attributes: &management.ConnectionOptionsAttributes{
Username: &management.ConnectionOptionsUsernameAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
},
},
Email: &management.ConnectionOptionsEmailAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
Verification: &management.ConnectionOptionsAttributeVerification{
Active: auth0.Bool(false),
},
},
},
PhoneNumber: &management.ConnectionOptionsPhoneNumberAttribute{
Identifier: &management.ConnectionOptionsAttributeIdentifier{
Active: auth0.Bool(true),
},
ProfileRequired: auth0.Bool(true),
Signup: &management.ConnectionOptionsAttributeSignup{
Status: auth0.String("required"),
Verification: &management.ConnectionOptionsAttributeVerification{
Active: auth0.Bool(false),
},
},
},
},
}

configureHTTPTestRecordings(t, authAPI)

details := givenSignUpDetails(t, connectionOptions)

userData := database.SignupRequest{
Connection: details.connection,
Username: details.username,
Password: details.password,
Email: details.email,
PhoneNumber: details.phoneNumber,
}

createdUser, err := authAPI.Database.Signup(context.Background(), userData)
require.NoError(t, err)

assert.NotEmpty(t, createdUser.ID)
assert.Equal(t, userData.Username, createdUser.Username)
assert.Equal(t, userData.Email, createdUser.Email)
assert.Equal(t, userData.PhoneNumber, createdUser.PhoneNumber)
}

// TestDatabaseChangePassword tests the Database.ChangePassword method.
func TestDatabaseChangePassword(t *testing.T) {
configureHTTPTestRecordings(t, authAPI)

Expand All @@ -46,43 +314,43 @@ func TestDatabaseChangePassword(t *testing.T) {
}

type userDetails struct {
username string
password string
email string
connection string
username string
password string
email string
connection string
phoneNumber string
}

func givenSignUpDetails(t *testing.T) *userDetails {
func givenSignUpDetails(t *testing.T, options *management.ConnectionOptions) *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",
username: "mytestaccount",
password: "mypassword",
email: "[email protected]",
phoneNumber: "+12345678900",
connection: "Username-Password-Authentication",
}
}

conn := givenAConnection(t)
conn := givenAConnection(t, options)

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(),
username: fmt.Sprintf("chuck%d", rand.Intn(999)),
password: "Passwords hide their chuck",
email: fmt.Sprintf("chuck%[email protected]", rand.Intn(999)),
phoneNumber: fmt.Sprintf("+1234567890%d", rand.Intn(9)),
connection: conn.GetName(),
}
}

func givenAConnection(t *testing.T) management.Connection {
func givenAConnection(t *testing.T, options *management.ConnectionOptions) *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),
},
}
conn.Options = &options

err := mgmtAPI.Connection.Create(context.Background(), conn)
require.NoError(t, err)
Expand All @@ -92,5 +360,5 @@ func givenAConnection(t *testing.T) management.Connection {
require.NoError(t, err)
})

return *conn
return conn
}
Loading

0 comments on commit e02f2a8

Please sign in to comment.