From 67eded337166684a5705e72ecfeca6b11159cd7c Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Wed, 13 Dec 2023 21:24:37 +0530 Subject: [PATCH] adds more tests and adds canlinkaccount recipe function --- ...accountlinkingRecipeImplementation_test.go | 49 +++++++++++++++++++ .../accountlinkingRecipeImplementation.go | 45 +++++++++++++++++ supertokens/main.go | 13 +++++ 3 files changed, 107 insertions(+) diff --git a/recipe/emailpassword/accountlinkingRecipeImplementation_test.go b/recipe/emailpassword/accountlinkingRecipeImplementation_test.go index c3157a44..4837c106 100644 --- a/recipe/emailpassword/accountlinkingRecipeImplementation_test.go +++ b/recipe/emailpassword/accountlinkingRecipeImplementation_test.go @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/supertokens/accountlinkingRecipeImplementation.go b/supertokens/accountlinkingRecipeImplementation.go index 19966922..b78befbd 100644 --- a/supertokens/accountlinkingRecipeImplementation.go +++ b/supertokens/accountlinkingRecipeImplementation.go @@ -279,6 +279,50 @@ 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, @@ -286,5 +330,6 @@ func makeRecipeImplementation(querier Querier, config AccountLinkingTypeNormalis CanCreatePrimaryUser: &canCreatePrimaryUser, CreatePrimaryUser: &createPrimaryUser, LinkAccounts: &linkAccounts, + CanLinkAccounts: &canLinkAccounts, } } diff --git a/supertokens/main.go b/supertokens/main.go index 88ca7399..0de28f18 100644 --- a/supertokens/main.go +++ b/supertokens/main.go @@ -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]) +}