Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Dec 15, 2023
1 parent 3e190bb commit b23246a
Showing 1 changed file with 334 additions and 0 deletions.
334 changes: 334 additions & 0 deletions recipe/emailpassword/accountlinkingRecipeImplementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b23246a

Please sign in to comment.