From c112c8a28bfc247f4d84f36f0d9522d3d15501c2 Mon Sep 17 00:00:00 2001 From: rishabhpoddar Date: Wed, 13 Dec 2023 14:11:36 +0530 Subject: [PATCH] more tests --- ...accountlinkingRecipeImplementation_test.go | 129 ++++++++++++++++++ recipe/thirdparty/recipeImplementation.go | 10 +- recipe/thirdparty/utils.go | 22 ++- 3 files changed, 156 insertions(+), 5 deletions(-) diff --git a/recipe/emailpassword/accountlinkingRecipeImplementation_test.go b/recipe/emailpassword/accountlinkingRecipeImplementation_test.go index aec3f0c5..56e5db94 100644 --- a/recipe/emailpassword/accountlinkingRecipeImplementation_test.go +++ b/recipe/emailpassword/accountlinkingRecipeImplementation_test.go @@ -20,6 +20,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels" + "github.com/supertokens/supertokens-golang/recipe/thirdparty" + "github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" "github.com/supertokens/supertokens-golang/supertokens" "github.com/supertokens/supertokens-golang/test/unittesting" ) @@ -646,6 +648,14 @@ func TestMakePrimaryFailCauseAlreadyLinkedToAnotherAccount(t *testing.T) { return } + canCreatePrimaryUserResponse, err := supertokens.CanCreatePrimaryUser(user2.LoginMethods[0].RecipeUserID) + if err != nil { + t.Error(err) + return + } + assert.Nil(t, canCreatePrimaryUserResponse.OK) + assert.Equal(t, canCreatePrimaryUserResponse.RecipeUserIdAlreadyLinkedWithPrimaryUserIdError.PrimaryUserId, user1.ID) + createPrimaryUserResponse, err := supertokens.CreatePrimaryUser(user2.LoginMethods[0].RecipeUserID) if err != nil { t.Error(err) @@ -655,6 +665,83 @@ func TestMakePrimaryFailCauseAlreadyLinkedToAnotherAccount(t *testing.T) { assert.Equal(t, createPrimaryUserResponse.RecipeUserIdAlreadyLinkedWithPrimaryUserIdError.PrimaryUserId, user1.ID) } +func TestMakePrimaryFailCauseAccountInfoAlreadyAssociatedWithAnotherPrimaryUser(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{ + Init(nil), + thirdparty.Init(&tpmodels.TypeInput{ + SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{ + Providers: []tpmodels.ProviderInput{ + { + Config: tpmodels.ProviderConfig{ + ThirdPartyId: "google", + Clients: []tpmodels.ProviderClientConfig{ + { + ClientID: "", + ClientSecret: "", + }, + }, + }, + }, + }, + }, + }), + }, + }) + + epuser, err := SignUp("public", "test@gmail.com", "pass123") + if err != nil { + t.Error(err) + return + } + + epuser1 := convertEpUserToSuperTokensUser(epuser.OK.User) + assert.False(t, epuser1.IsPrimaryUser) + + tpuser, err := thirdparty.ManuallyCreateOrUpdateUser("public", "google", "abc", "test@gmail.com") + if err != nil { + t.Error(err) + return + } + + tpUser1 := convertTpUserToSuperTokensUser(tpuser.OK.User) + + _, err = supertokens.CreatePrimaryUser(tpUser1.LoginMethods[0].RecipeUserID) + if err != nil { + t.Error(err) + return + } + + canCreatePrimaryUserResult, err := supertokens.CanCreatePrimaryUser(epuser1.LoginMethods[0].RecipeUserID) + if err != nil { + t.Error(err) + return + } + assert.Nil(t, canCreatePrimaryUserResult.OK) + assert.Equal(t, canCreatePrimaryUserResult.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError.PrimaryUserId, tpUser1.ID) + + createPrimaryUserResponse, err := supertokens.CreatePrimaryUser(epuser1.LoginMethods[0].RecipeUserID) + if err != nil { + t.Error(err) + return + } + assert.Nil(t, createPrimaryUserResponse.OK) + assert.Equal(t, createPrimaryUserResponse.AccountInfoAlreadyAssociatedWithAnotherPrimaryUserIdError.PrimaryUserId, tpUser1.ID) +} + // TODO: remove this function func convertEpUserToSuperTokensUser(epuser epmodels.User) supertokens.User { rUId, err := supertokens.NewRecipeUserID(epuser.ID) @@ -687,3 +774,45 @@ func convertEpUserToSuperTokensUser(epuser epmodels.User) supertokens.User { }, } } + +// TODO: remove this function +func convertTpUserToSuperTokensUser(tpuser tpmodels.User) supertokens.User { + rUId, err := supertokens.NewRecipeUserID(tpuser.ID) + if err != nil { + panic(err.Error()) + } + return supertokens.User{ + ID: tpuser.ID, + TimeJoined: tpuser.TimeJoined, + IsPrimaryUser: false, + TenantIDs: tpuser.TenantIds, + Emails: []string{tpuser.Email}, + PhoneNumbers: []string{}, + ThirdParty: []supertokens.ThirdParty{ + { + ID: tpuser.ThirdParty.ID, + UserID: tpuser.ThirdParty.UserID, + }, + }, + LoginMethods: []supertokens.LoginMethods{ + { + Verified: false, + RecipeLevelUser: supertokens.RecipeLevelUser{ + TenantIDs: tpuser.TenantIds, + TimeJoined: tpuser.TimeJoined, + RecipeUserID: rUId, + AccountInfoWithRecipeID: supertokens.AccountInfoWithRecipeID{ + RecipeID: supertokens.EmailPasswordRID, + AccountInfo: supertokens.AccountInfo{ + Email: &tpuser.Email, + ThirdParty: &supertokens.ThirdParty{ + ID: tpuser.ThirdParty.ID, + UserID: tpuser.ThirdParty.UserID, + }, + }, + }, + }, + }, + }, + } +} diff --git a/recipe/thirdparty/recipeImplementation.go b/recipe/thirdparty/recipeImplementation.go index b499b7ca..5b7103af 100644 --- a/recipe/thirdparty/recipeImplementation.go +++ b/recipe/thirdparty/recipeImplementation.go @@ -50,7 +50,10 @@ func MakeRecipeImplementation(querier supertokens.Querier, providers []tpmodels. response, err := querier.SendPostRequest(tenantId+"/recipe/signinup", map[string]interface{}{ "thirdPartyId": thirdPartyID, "thirdPartyUserId": thirdPartyUserID, - "email": map[string]interface{}{"id": email}, + "email": map[string]interface{}{ + "id": email, + "isVerified": false, // TODO: properly implement this + }, }, userContext) if err != nil { return tpmodels.SignInUpResponse{}, err @@ -78,7 +81,10 @@ func MakeRecipeImplementation(querier supertokens.Querier, providers []tpmodels. response, err := querier.SendPostRequest(tenantId+"/recipe/signinup", map[string]interface{}{ "thirdPartyId": thirdPartyID, "thirdPartyUserId": thirdPartyUserID, - "email": map[string]interface{}{"id": email}, + "email": map[string]interface{}{ + "id": email, + "isVerified": false, // TODO: properly implement this + }, }, userContext) if err != nil { return tpmodels.ManuallyCreateOrUpdateUserResponse{}, err diff --git a/recipe/thirdparty/utils.go b/recipe/thirdparty/utils.go index dbef81d1..1320e21d 100644 --- a/recipe/thirdparty/utils.go +++ b/recipe/thirdparty/utils.go @@ -84,12 +84,28 @@ func parseUser(value interface{}) (*tpmodels.User, error) { if err != nil { return nil, err } - var user tpmodels.User - err = json.Unmarshal(respJSON, &user) + var supertokensUser supertokens.User + err = json.Unmarshal(respJSON, &supertokensUser) + if err != nil { return nil, err } - return &user, nil + + tpUser := tpmodels.User{ + ID: supertokensUser.ID, + Email: supertokensUser.Emails[0], + TimeJoined: supertokensUser.TimeJoined, + TenantIds: supertokensUser.TenantIDs, + ThirdParty: struct { + ID string `json:"id"` + UserID string `json:"userId"` + }{ + ID: supertokensUser.ThirdParty[0].ID, + UserID: supertokensUser.ThirdParty[0].UserID, + }, + } + + return &tpUser, nil } func parseUsers(value interface{}) ([]tpmodels.User, error) {