Skip to content

Commit 0e17deb

Browse files
committed
adds more tests
1 parent c6ffd38 commit 0e17deb

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

recipe/emailpassword/accountlinkingRecipeImplementation_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/stretchr/testify/assert"
2222
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
23+
"github.com/supertokens/supertokens-golang/recipe/session"
2324
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
2425
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
2526
"github.com/supertokens/supertokens-golang/supertokens"
@@ -742,6 +743,90 @@ func TestMakePrimaryFailCauseAccountInfoAlreadyAssociatedWithAnotherPrimaryUser(
742743
assert.Equal(t, createPrimaryUserResponse.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError.PrimaryUserId, tpUser1.ID)
743744
}
744745

746+
func TestLinkAccountsSuccess(t *testing.T) {
747+
BeforeEach()
748+
unittesting.StartUpST("localhost", "8080")
749+
defer AfterEach()
750+
telemetry := false
751+
var primaryUserInCallback supertokens.User
752+
var newAccountInfoInCallback supertokens.RecipeLevelUser
753+
supertokens.Init(supertokens.TypeInput{
754+
Supertokens: &supertokens.ConnectionInfo{
755+
ConnectionURI: "http://localhost:8080",
756+
},
757+
AppInfo: supertokens.AppInfo{
758+
AppName: "Testing",
759+
Origin: "http://localhost:3000",
760+
APIDomain: "http://localhost:3001",
761+
},
762+
Telemetry: &telemetry,
763+
RecipeList: []supertokens.Recipe{
764+
session.Init(nil),
765+
Init(nil),
766+
supertokens.InitAccountLinking(&supertokens.AccountLinkingTypeInput{
767+
OnAccountLinked: func(user supertokens.User, newAccountUser supertokens.RecipeLevelUser, userContext supertokens.UserContext) error {
768+
primaryUserInCallback = user
769+
newAccountInfoInCallback = newAccountUser
770+
return nil
771+
},
772+
}),
773+
},
774+
})
775+
776+
epuser, err := SignUp("public", "[email protected]", "pass123")
777+
if err != nil {
778+
t.Error(err)
779+
return
780+
}
781+
782+
user1 := convertEpUserToSuperTokensUser(epuser.OK.User)
783+
assert.False(t, user1.IsPrimaryUser)
784+
supertokens.CreatePrimaryUser(user1.LoginMethods[0].RecipeUserID)
785+
786+
epuser2, err := SignUp("public", "[email protected]", "pass123")
787+
if err != nil {
788+
t.Error(err)
789+
return
790+
}
791+
792+
user2 := convertEpUserToSuperTokensUser(epuser2.OK.User)
793+
assert.False(t, user2.IsPrimaryUser)
794+
795+
// we create a new session to check that the session has not been revoked
796+
// when we link accounts, cause these users are already linked.
797+
session.CreateNewSessionWithoutRequestResponse("public", user2.ID, nil, nil, nil)
798+
sessions, err := session.GetAllSessionHandlesForUser(user2.ID, nil)
799+
if err != nil {
800+
t.Error(err)
801+
return
802+
}
803+
assert.Len(t, sessions, 1)
804+
805+
linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID)
806+
if err != nil {
807+
t.Error(err)
808+
return
809+
}
810+
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked)
811+
812+
linkedUser, err := supertokens.GetUser(user1.ID)
813+
if err != nil {
814+
t.Error(err)
815+
return
816+
}
817+
assert.Equal(t, *linkedUser, primaryUserInCallback)
818+
assert.Equal(t, *linkedUser, linkAccountResponse.OK.User)
819+
820+
assert.Equal(t, newAccountInfoInCallback.RecipeID, supertokens.EmailPasswordRID)
821+
assert.Equal(t, *newAccountInfoInCallback.Email, "[email protected]")
822+
sessions, err = session.GetAllSessionHandlesForUser(user2.LoginMethods[0].RecipeUserID.GetAsString(), nil)
823+
if err != nil {
824+
t.Error(err)
825+
return
826+
}
827+
assert.Len(t, sessions, 0)
828+
}
829+
745830
// TODO: remove this function
746831
func convertEpUserToSuperTokensUser(epuser epmodels.User) supertokens.User {
747832
rUId, err := supertokens.NewRecipeUserID(epuser.ID)

0 commit comments

Comments
 (0)