Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Dec 13, 2023
1 parent 9b7223c commit c112c8a
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 5 deletions.
129 changes: 129 additions & 0 deletions recipe/emailpassword/accountlinkingRecipeImplementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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", "[email protected]", "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", "[email protected]")
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)
Expand Down Expand Up @@ -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,
},
},
},
},
},
},
}
}
10 changes: 8 additions & 2 deletions recipe/thirdparty/recipeImplementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions recipe/thirdparty/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c112c8a

Please sign in to comment.