Skip to content

Commit

Permalink
adds more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Dec 13, 2023
1 parent c6ffd38 commit 0e17deb
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions recipe/emailpassword/accountlinkingRecipeImplementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
"github.com/supertokens/supertokens-golang/recipe/session"
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
"github.com/supertokens/supertokens-golang/supertokens"
Expand Down Expand Up @@ -742,6 +743,90 @@ func TestMakePrimaryFailCauseAccountInfoAlreadyAssociatedWithAnotherPrimaryUser(
assert.Equal(t, createPrimaryUserResponse.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError.PrimaryUserId, tpUser1.ID)
}

func TestLinkAccountsSuccess(t *testing.T) {
BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
telemetry := false
var primaryUserInCallback supertokens.User
var newAccountInfoInCallback supertokens.RecipeLevelUser
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),
supertokens.InitAccountLinking(&supertokens.AccountLinkingTypeInput{
OnAccountLinked: func(user supertokens.User, newAccountUser supertokens.RecipeLevelUser, userContext supertokens.UserContext) error {
primaryUserInCallback = user
newAccountInfoInCallback = newAccountUser
return nil
},
}),
},
})

epuser, err := SignUp("public", "[email protected]", "pass123")
if err != nil {
t.Error(err)
return
}

user1 := convertEpUserToSuperTokensUser(epuser.OK.User)
assert.False(t, user1.IsPrimaryUser)
supertokens.CreatePrimaryUser(user1.LoginMethods[0].RecipeUserID)

epuser2, err := SignUp("public", "[email protected]", "pass123")
if err != nil {
t.Error(err)
return
}

user2 := convertEpUserToSuperTokensUser(epuser2.OK.User)
assert.False(t, user2.IsPrimaryUser)

// we create a new session to check that the session has not been revoked
// when we link accounts, cause these users are already linked.
session.CreateNewSessionWithoutRequestResponse("public", user2.ID, nil, nil, nil)
sessions, err := session.GetAllSessionHandlesForUser(user2.ID, nil)
if err != nil {
t.Error(err)
return
}
assert.Len(t, sessions, 1)

linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID)
if err != nil {
t.Error(err)
return
}
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked)

linkedUser, err := supertokens.GetUser(user1.ID)
if err != nil {
t.Error(err)
return
}
assert.Equal(t, *linkedUser, primaryUserInCallback)
assert.Equal(t, *linkedUser, linkAccountResponse.OK.User)

assert.Equal(t, newAccountInfoInCallback.RecipeID, supertokens.EmailPasswordRID)
assert.Equal(t, *newAccountInfoInCallback.Email, "[email protected]")
sessions, err = session.GetAllSessionHandlesForUser(user2.LoginMethods[0].RecipeUserID.GetAsString(), nil)
if err != nil {
t.Error(err)
return
}
assert.Len(t, sessions, 0)
}

// TODO: remove this function
func convertEpUserToSuperTokensUser(epuser epmodels.User) supertokens.User {
rUId, err := supertokens.NewRecipeUserID(epuser.ID)
Expand Down

0 comments on commit 0e17deb

Please sign in to comment.