Skip to content

Commit

Permalink
moves more code around
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Apr 10, 2024
1 parent 325bfae commit 102b63a
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 1,150 deletions.
169 changes: 169 additions & 0 deletions recipe/passwordless/recipeFucntions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,182 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/supertokens/supertokens-golang/recipe/emailverification"
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
"github.com/supertokens/supertokens-golang/recipe/session"
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels"
"github.com/supertokens/supertokens-golang/supertokens"
"github.com/supertokens/supertokens-golang/test/unittesting"
)

func TestWithThirdPartyPasswordlessGetUserFunctionality(t *testing.T) {
configValue := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "http://localhost:8080",
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
},
RecipeList: []supertokens.Recipe{
session.Init(&sessmodels.TypeInput{
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
return sessmodels.CookieTransferMethod
},
}),
Init(plessmodels.TypeInput{
FlowType: "USER_INPUT_CODE_AND_MAGIC_LINK",
ContactMethodEmailOrPhone: plessmodels.ContactMethodEmailOrPhoneConfig{
Enabled: true,
},
}),
},
}

BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
err := supertokens.Init(configValue)
if err != nil {
t.Error(err.Error())
}
q, err := supertokens.GetNewQuerierInstanceOrThrowError("")
if err != nil {
t.Error(err.Error())
}
apiV, err := q.GetQuerierAPIVersion()
if err != nil {
t.Error(err.Error())
}

if unittesting.MaxVersion(apiV, "2.11") == "2.11" {
return
}

user, err := GetUserByID("random")
assert.NoError(t, err)
assert.Nil(t, user)

resp, err := SignInUpByEmail("public", "[email protected]")
assert.NoError(t, err)
userId := resp.User.ID

user, err = GetUserByID(userId)
assert.NoError(t, err)
assert.NotNil(t, user)

assert.Equal(t, userId, user.ID)
assert.Equal(t, resp.User.Email, user.Email)
assert.Nil(t, user.PhoneNumber)

users, err := GetUserByEmail("public", "random")
assert.NoError(t, err)
assert.Nil(t, users)

users, err = GetUserByEmail("public", "[email protected]")
assert.NoError(t, err)
assert.NotNil(t, users)

userInfo := users

assert.Equal(t, user.Email, userInfo.Email)
assert.Equal(t, user.ID, userInfo.ID)
assert.Equal(t, user.PhoneNumber, userInfo.PhoneNumber)
assert.Nil(t, userInfo.PhoneNumber)
assert.Equal(t, user.TimeJoined, userInfo.TimeJoined)

user, err = GetUserByPhoneNumber("public", "random")
assert.NoError(t, err)
assert.Nil(t, user)

resp, err = SignInUpByPhoneNumber("public", "+1234567890")
assert.NoError(t, err)

user, err = GetUserByPhoneNumber("public", *resp.User.PhoneNumber)
assert.NoError(t, err)
assert.NotNil(t, user)

assert.Equal(t, user.Email, resp.User.Email)
assert.Equal(t, user.ID, resp.User.ID)
assert.Equal(t, user.PhoneNumber, resp.User.PhoneNumber)
assert.Nil(t, user.Email)
}

func TestForPasswordlessUserThatIsEmailVerifiedReturnsTrueForPhoneAndFalseForEmail(t *testing.T) {
configValue := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "http://localhost:8080",
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
},
RecipeList: []supertokens.Recipe{
emailverification.Init(evmodels.TypeInput{
Mode: evmodels.ModeOptional,
}),
session.Init(&sessmodels.TypeInput{
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
return sessmodels.CookieTransferMethod
},
}),
Init(plessmodels.TypeInput{
FlowType: "USER_INPUT_CODE_AND_MAGIC_LINK",
ContactMethodEmailOrPhone: plessmodels.ContactMethodEmailOrPhoneConfig{
Enabled: true,
},
}),
},
}

BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
err := supertokens.Init(configValue)
if err != nil {
t.Error(err.Error())
}
q, err := supertokens.GetNewQuerierInstanceOrThrowError("")
if err != nil {
t.Error(err.Error())
}
apiV, err := q.GetQuerierAPIVersion()
if err != nil {
t.Error(err.Error())
}

if unittesting.MaxVersion(apiV, "2.11") == "2.11" {
return
}

response, err := SignInUpByEmail("public", "[email protected]")
assert.NoError(t, err)

isVerified, err := emailverification.IsEmailVerified(response.User.ID, nil)
assert.NoError(t, err)
assert.False(t, isVerified) // this is a change in behavior

emailVerificationResp, err := emailverification.CreateEmailVerificationToken("public", response.User.ID, nil)
assert.NoError(t, err)
assert.Nil(t, emailVerificationResp.EmailAlreadyVerifiedError)
assert.NotNil(t, emailVerificationResp.OK)

response, err = SignInUpByPhoneNumber("public", "+123456789012")
assert.NoError(t, err)

isVerified, err = emailverification.IsEmailVerified(response.User.ID, nil)
assert.NoError(t, err)
assert.True(t, isVerified)

emailVerificationResp, err = emailverification.CreateEmailVerificationToken("public", response.User.ID, nil)
assert.NoError(t, err)
assert.NotNil(t, emailVerificationResp.EmailAlreadyVerifiedError)
assert.Nil(t, emailVerificationResp.OK)
}

func TestGetUser(t *testing.T) {
configValue := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
Expand Down
69 changes: 69 additions & 0 deletions recipe/thirdparty/signinupFeature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,75 @@ import (
"gopkg.in/h2non/gock.v1"
)

func TestThatThirdPartyUserThatIsEmailVerifiedReturnsTheCorrectEmailVerificationStatus(t *testing.T) {
configValue := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
ConnectionURI: "http://localhost:8080",
},
AppInfo: supertokens.AppInfo{
APIDomain: "api.supertokens.io",
AppName: "SuperTokens",
WebsiteDomain: "supertokens.io",
},
RecipeList: []supertokens.Recipe{
emailverification.Init(evmodels.TypeInput{
Mode: evmodels.ModeOptional,
}),
session.Init(&sessmodels.TypeInput{
GetTokenTransferMethod: func(req *http.Request, forCreateNewSession bool, userContext supertokens.UserContext) sessmodels.TokenTransferMethod {
return sessmodels.CookieTransferMethod
},
}),
Init(&tpmodels.TypeInput{
SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{
Providers: []tpmodels.ProviderInput{
customProvider1,
},
},
}),
},
}

BeforeEach()
unittesting.StartUpST("localhost", "8080")
defer AfterEach()
err := supertokens.Init(configValue)
if err != nil {
t.Error(err.Error())
}
q, err := supertokens.GetNewQuerierInstanceOrThrowError("")
if err != nil {
t.Error(err.Error())
}
apiV, err := q.GetQuerierAPIVersion()
if err != nil {
t.Error(err.Error())
}

if unittesting.MaxVersion(apiV, "2.11") == "2.11" {
return
}

resp, err := ManuallyCreateOrUpdateUser("public", "custom", "verifiedUser", "[email protected]")
assert.NoError(t, err)

emailVerificationToken, err := emailverification.CreateEmailVerificationToken("public", resp.OK.User.ID, nil)
assert.NoError(t, err)

emailverification.VerifyEmailUsingToken("public", emailVerificationToken.OK.Token)

isVerfied, err := emailverification.IsEmailVerified(resp.OK.User.ID, nil)
assert.NoError(t, err)
assert.True(t, isVerfied)

resp1, err := ManuallyCreateOrUpdateUser("public", "custom2", "NotVerifiedUser", "[email protected]")
assert.NoError(t, err)

isVerfied1, err := emailverification.IsEmailVerified(resp1.OK.User.ID, nil)
assert.NoError(t, err)
assert.False(t, isVerfied1)
}

func TestWithDisabledAPIDefaultSigninupAPIdoesnNotWork(t *testing.T) {
configValue := supertokens.TypeInput{
Supertokens: &supertokens.ConnectionInfo{
Expand Down
Loading

0 comments on commit 102b63a

Please sign in to comment.