Skip to content

Commit

Permalink
adds more tests and adds canlinkaccount recipe function
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Dec 13, 2023
1 parent ff917ce commit 67eded3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
49 changes: 49 additions & 0 deletions recipe/emailpassword/accountlinkingRecipeImplementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,15 @@ func TestLinkAccountsSuccess(t *testing.T) {
}
assert.Len(t, sessions, 1)

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

linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -875,6 +884,15 @@ func TestLinkAccountsSuccessAlreadyLinked(t *testing.T) {
}
assert.False(t, linkAccountResponse.OK.AccountsAlreadyLinked)

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

linkAccountResponse, err = supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -950,6 +968,17 @@ func TestLinkAccountsFailureAlreadyLinkedWithAnotherPrimaryUser(t *testing.T) {
assert.False(t, user3.IsPrimaryUser)
supertokens.CreatePrimaryUser(user3.LoginMethods[0].RecipeUserID)

{
canLinkAccountResponse, err := supertokens.CanLinkAccounts(user2.LoginMethods[0].RecipeUserID, user3.ID)
if err != nil {
t.Error(err)
return
}
assert.Nil(t, canLinkAccountResponse.OK)
assert.NotNil(t, canLinkAccountResponse.RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdError)
assert.Equal(t, canLinkAccountResponse.RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdError.PrimaryUserId, user1.ID)
}

linkAccountResponse, err = supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user3.ID)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -1002,6 +1031,16 @@ func TestLinkAccountsFailureInputUserIdNotAPrimaryUser(t *testing.T) {
user2 := convertEpUserToSuperTokensUser(epuser2.OK.User)
assert.False(t, user2.IsPrimaryUser)

{
linkAccountResponse, err := supertokens.CanLinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID)
if err != nil {
t.Error(err)
return
}
assert.Nil(t, linkAccountResponse.OK)
assert.NotNil(t, linkAccountResponse.InputUserIsNotAPrimaryUserError)
}

linkAccountResponse, err := supertokens.LinkAccounts(user2.LoginMethods[0].RecipeUserID, user1.ID)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -1075,6 +1114,16 @@ func TestLinkAccountFailureAccountInfoAlreadyAssociatedWithAnotherPrimaryUser(t
epuser2 := convertEpUserToSuperTokensUser(epuser.OK.User)
supertokens.CreatePrimaryUser(epuser2.LoginMethods[0].RecipeUserID)

{
linkAccountResponse, err := supertokens.CanLinkAccounts(tpUser1.LoginMethods[0].RecipeUserID, epuser2.ID)
if err != nil {
t.Error(err)
return
}
assert.NotNil(t, linkAccountResponse.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError)
assert.Equal(t, linkAccountResponse.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError.PrimaryUserId, epuser1.ID)
}

linkAccountResponse, err := supertokens.LinkAccounts(tpUser1.LoginMethods[0].RecipeUserID, epuser2.ID)
if err != nil {
t.Error(err)
Expand Down
45 changes: 45 additions & 0 deletions supertokens/accountlinkingRecipeImplementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,57 @@ func makeRecipeImplementation(querier Querier, config AccountLinkingTypeNormalis
}
}

canLinkAccounts := func(recipeUserId RecipeUserID, primaryUserId string, userContext UserContext) (CanLinkAccountResponse, error) {
requestBody := map[string]string{
"recipeUserId": recipeUserId.GetAsString(),
"primaryUserId": primaryUserId,
}
resp, err := querier.SendGetRequest("/recipe/accountlinking/user/link/check", requestBody, userContext)

if err != nil {
return CanLinkAccountResponse{}, err
}

if resp["status"].(string) == "OK" {
return CanLinkAccountResponse{
OK: &struct{ AccountsAlreadyLinked bool }{
AccountsAlreadyLinked: resp["accountsAlreadyLinked"].(bool),
},
}, nil
} else if resp["status"].(string) == "RECIPE_USER_ID_ALREADY_LINKED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" {
return CanLinkAccountResponse{
RecipeUserIdAlreadyLinkedWithAnotherPrimaryUserIdError: &struct {
PrimaryUserId string
Description string
}{
PrimaryUserId: resp["primaryUserId"].(string),
Description: resp["description"].(string),
},
}, nil
} else if resp["status"].(string) == "ACCOUNT_INFO_ALREADY_ASSOCIATED_WITH_ANOTHER_PRIMARY_USER_ID_ERROR" {
return CanLinkAccountResponse{
AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError: &struct {
PrimaryUserId string
Description string
}{
PrimaryUserId: resp["primaryUserId"].(string),
Description: resp["description"].(string),
},
}, nil
} else {
return CanLinkAccountResponse{
InputUserIsNotAPrimaryUserError: &struct{}{},
}, nil
}
}

// TODO:...
return AccountLinkingRecipeInterface{
GetUsersWithSearchParams: &getUsers,
GetUser: &getUser,
CanCreatePrimaryUser: &canCreatePrimaryUser,
CreatePrimaryUser: &createPrimaryUser,
LinkAccounts: &linkAccounts,
CanLinkAccounts: &canLinkAccounts,
}
}
13 changes: 13 additions & 0 deletions supertokens/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,16 @@ func LinkAccounts(recipeUserId RecipeUserID, primaryUserId string, userContext .

return (*accountLinkingInstance.RecipeImpl.LinkAccounts)(recipeUserId, primaryUserId, userContext[0])
}

func CanLinkAccounts(recipeUserId RecipeUserID, primaryUserId string, userContext ...UserContext) (CanLinkAccountResponse, error) {
accountLinkingInstance, err := getAccountLinkingRecipeInstanceOrThrowError()
if err != nil {
return CanLinkAccountResponse{}, err
}

if len(userContext) == 0 {
userContext = append(userContext, &map[string]interface{}{})
}

return (*accountLinkingInstance.RecipeImpl.CanLinkAccounts)(recipeUserId, primaryUserId, userContext[0])
}

0 comments on commit 67eded3

Please sign in to comment.