-
Notifications
You must be signed in to change notification settings - Fork 36
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
1 parent
3e190bb
commit b23246a
Showing
1 changed file
with
334 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ import ( | |
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels" | ||
"github.com/supertokens/supertokens-golang/recipe/emailverification" | ||
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels" | ||
"github.com/supertokens/supertokens-golang/recipe/session" | ||
"github.com/supertokens/supertokens-golang/recipe/thirdparty" | ||
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" | ||
|
@@ -1323,6 +1325,338 @@ func TestDeleteUser(t *testing.T) { | |
assert.Equal(t, 0, len(responseAfterDeletingUser.Users)) | ||
} | ||
|
||
func TestLinkAccountCausesNewAccountsEmailToBeVerifiedIfSameEmail(t *testing.T) { | ||
BeforeEach() | ||
unittesting.StartUpST("localhost", "8080") | ||
defer AfterEach() | ||
telemetry := false | ||
supertokens.Init(supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "Testing", | ||
Origin: "http://localhost:3000", | ||
APIDomain: "http://localhost:3001", | ||
}, | ||
Telemetry: &telemetry, | ||
RecipeList: []supertokens.Recipe{ | ||
session.Init(nil), | ||
Init(nil), | ||
emailverification.Init(evmodels.TypeInput{ | ||
Mode: evmodels.ModeRequired, | ||
}), | ||
thirdparty.Init(&tpmodels.TypeInput{ | ||
SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{ | ||
Providers: []tpmodels.ProviderInput{ | ||
{ | ||
Config: tpmodels.ProviderConfig{ | ||
ThirdPartyId: "google", | ||
Clients: []tpmodels.ProviderClientConfig{ | ||
{ | ||
ClientID: "", | ||
ClientSecret: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}) | ||
|
||
tpUser, err := thirdparty.ManuallyCreateOrUpdateUser("public", "google", "abc", "[email protected]") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
user1 := convertTpUserToSuperTokensUser(tpUser.OK.User) | ||
|
||
token, err := emailverification.CreateEmailVerificationToken("public", user1.ID, nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
_, err = emailverification.VerifyEmailUsingToken("public", token.OK.Token) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
epuser, err := SignUp("public", "[email protected]", "pass123") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
user2 := convertEpUserToSuperTokensUser(epuser.OK.User) | ||
assert.False(t, user1.IsPrimaryUser) | ||
|
||
supertokens.CreatePrimaryUser(user1.LoginMethods[0].RecipeUserID) | ||
|
||
linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked) | ||
|
||
isEmailVerified, err := emailverification.IsEmailVerified(user2.LoginMethods[0].RecipeUserID.GetAsString(), nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.True(t, isEmailVerified) | ||
} | ||
|
||
func TestLinkAccountDoesNotCausePrimaryAccountsEmailToBeVerifiedIfSameEmail(t *testing.T) { | ||
BeforeEach() | ||
unittesting.StartUpST("localhost", "8080") | ||
defer AfterEach() | ||
telemetry := false | ||
supertokens.Init(supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "Testing", | ||
Origin: "http://localhost:3000", | ||
APIDomain: "http://localhost:3001", | ||
}, | ||
Telemetry: &telemetry, | ||
RecipeList: []supertokens.Recipe{ | ||
session.Init(nil), | ||
Init(nil), | ||
emailverification.Init(evmodels.TypeInput{ | ||
Mode: evmodels.ModeRequired, | ||
}), | ||
thirdparty.Init(&tpmodels.TypeInput{ | ||
SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{ | ||
Providers: []tpmodels.ProviderInput{ | ||
{ | ||
Config: tpmodels.ProviderConfig{ | ||
ThirdPartyId: "google", | ||
Clients: []tpmodels.ProviderClientConfig{ | ||
{ | ||
ClientID: "", | ||
ClientSecret: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}) | ||
|
||
tpUser, err := thirdparty.ManuallyCreateOrUpdateUser("public", "google", "abc", "[email protected]") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
user1 := convertTpUserToSuperTokensUser(tpUser.OK.User) | ||
|
||
epuser, err := SignUp("public", "[email protected]", "pass123") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
user2 := convertEpUserToSuperTokensUser(epuser.OK.User) | ||
assert.False(t, user1.IsPrimaryUser) | ||
|
||
token, err := emailverification.CreateEmailVerificationToken("public", user2.ID, nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
_, err = emailverification.VerifyEmailUsingToken("public", token.OK.Token) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
supertokens.CreatePrimaryUser(user1.LoginMethods[0].RecipeUserID) | ||
|
||
linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked) | ||
|
||
isEmailVerified, err := emailverification.IsEmailVerified(user1.LoginMethods[0].RecipeUserID.GetAsString(), nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, isEmailVerified) | ||
} | ||
|
||
func TestLinkAccountDoesNotCauseNewAccountsEmailToBeVerifiedIfDifferentEmail(t *testing.T) { | ||
BeforeEach() | ||
unittesting.StartUpST("localhost", "8080") | ||
defer AfterEach() | ||
telemetry := false | ||
supertokens.Init(supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "Testing", | ||
Origin: "http://localhost:3000", | ||
APIDomain: "http://localhost:3001", | ||
}, | ||
Telemetry: &telemetry, | ||
RecipeList: []supertokens.Recipe{ | ||
session.Init(nil), | ||
Init(nil), | ||
emailverification.Init(evmodels.TypeInput{ | ||
Mode: evmodels.ModeRequired, | ||
}), | ||
thirdparty.Init(&tpmodels.TypeInput{ | ||
SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{ | ||
Providers: []tpmodels.ProviderInput{ | ||
{ | ||
Config: tpmodels.ProviderConfig{ | ||
ThirdPartyId: "google", | ||
Clients: []tpmodels.ProviderClientConfig{ | ||
{ | ||
ClientID: "", | ||
ClientSecret: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}) | ||
|
||
tpUser, err := thirdparty.ManuallyCreateOrUpdateUser("public", "google", "abc", "[email protected]") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
user1 := convertTpUserToSuperTokensUser(tpUser.OK.User) | ||
|
||
token, err := emailverification.CreateEmailVerificationToken("public", user1.ID, nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
_, err = emailverification.VerifyEmailUsingToken("public", token.OK.Token) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
epuser, err := SignUp("public", "[email protected]", "pass123") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
user2 := convertEpUserToSuperTokensUser(epuser.OK.User) | ||
assert.False(t, user1.IsPrimaryUser) | ||
|
||
supertokens.CreatePrimaryUser(user1.LoginMethods[0].RecipeUserID) | ||
|
||
linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked) | ||
|
||
isEmailVerified, err := emailverification.IsEmailVerified(user2.LoginMethods[0].RecipeUserID.GetAsString(), nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, isEmailVerified) | ||
} | ||
|
||
func TestLinkAccountDoesNotCauseNewAccountsEmailToBeVerifiedIfSameEmailButPrimaryIsNotVerified(t *testing.T) { | ||
BeforeEach() | ||
unittesting.StartUpST("localhost", "8080") | ||
defer AfterEach() | ||
telemetry := false | ||
supertokens.Init(supertokens.TypeInput{ | ||
Supertokens: &supertokens.ConnectionInfo{ | ||
ConnectionURI: "http://localhost:8080", | ||
}, | ||
AppInfo: supertokens.AppInfo{ | ||
AppName: "Testing", | ||
Origin: "http://localhost:3000", | ||
APIDomain: "http://localhost:3001", | ||
}, | ||
Telemetry: &telemetry, | ||
RecipeList: []supertokens.Recipe{ | ||
session.Init(nil), | ||
Init(nil), | ||
emailverification.Init(evmodels.TypeInput{ | ||
Mode: evmodels.ModeRequired, | ||
}), | ||
thirdparty.Init(&tpmodels.TypeInput{ | ||
SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{ | ||
Providers: []tpmodels.ProviderInput{ | ||
{ | ||
Config: tpmodels.ProviderConfig{ | ||
ThirdPartyId: "google", | ||
Clients: []tpmodels.ProviderClientConfig{ | ||
{ | ||
ClientID: "", | ||
ClientSecret: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}) | ||
|
||
tpUser, err := thirdparty.ManuallyCreateOrUpdateUser("public", "google", "abc", "[email protected]") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
user1 := convertTpUserToSuperTokensUser(tpUser.OK.User) | ||
|
||
epuser, err := SignUp("public", "[email protected]", "pass123") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
user2 := convertEpUserToSuperTokensUser(epuser.OK.User) | ||
assert.False(t, user1.IsPrimaryUser) | ||
|
||
supertokens.CreatePrimaryUser(user1.LoginMethods[0].RecipeUserID) | ||
|
||
linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked) | ||
|
||
isEmailVerified, err := emailverification.IsEmailVerified(user2.LoginMethods[0].RecipeUserID.GetAsString(), nil) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
assert.False(t, isEmailVerified) | ||
} | ||
|
||
// TODO: remove this function | ||
func convertEpUserToSuperTokensUser(epuser epmodels.User) supertokens.User { | ||
rUId, err := supertokens.NewRecipeUserID(epuser.ID) | ||
|