|
| 1 | +package thirdpartypasswordless |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels" |
| 8 | + "github.com/supertokens/supertokens-golang/recipe/thirdparty" |
| 9 | + "github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels" |
| 10 | + "github.com/supertokens/supertokens-golang/recipe/thirdpartypasswordless/tplmodels" |
| 11 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 12 | + "github.com/supertokens/supertokens-golang/test/unittesting" |
| 13 | +) |
| 14 | + |
| 15 | +func initForUserIdMappingTest(t *testing.T) { |
| 16 | + |
| 17 | + config := supertokens.TypeInput{ |
| 18 | + Supertokens: &supertokens.ConnectionInfo{ |
| 19 | + ConnectionURI: "http://localhost:8080", |
| 20 | + }, |
| 21 | + AppInfo: supertokens.AppInfo{ |
| 22 | + APIDomain: "api.supertokens.io", |
| 23 | + AppName: "SuperTokens", |
| 24 | + WebsiteDomain: "supertokens.io", |
| 25 | + }, |
| 26 | + RecipeList: []supertokens.Recipe{Init(tplmodels.TypeInput{ |
| 27 | + ContactMethodEmailOrPhone: plessmodels.ContactMethodEmailOrPhoneConfig{ |
| 28 | + Enabled: true, |
| 29 | + }, |
| 30 | + FlowType: "USER_INPUT_CODE_AND_MAGIC_LINK", |
| 31 | + Providers: []tpmodels.TypeProvider{ |
| 32 | + thirdparty.Google(tpmodels.GoogleConfig{ClientID: "clientID", ClientSecret: "clientSecret"}), |
| 33 | + }, |
| 34 | + })}, |
| 35 | + } |
| 36 | + |
| 37 | + err := supertokens.Init(config) |
| 38 | + assert.NoError(t, err) |
| 39 | +} |
| 40 | + |
| 41 | +func TestCreateUserIdMappingUsingEmail(t *testing.T) { |
| 42 | + BeforeEach() |
| 43 | + unittesting.StartUpST("localhost", "8080") |
| 44 | + defer AfterEach() |
| 45 | + |
| 46 | + initForUserIdMappingTest(t) |
| 47 | + |
| 48 | + querier, err := supertokens.GetNewQuerierInstanceOrThrowError("") |
| 49 | + assert.NoError(t, err) |
| 50 | + |
| 51 | + cdiVersion, err := querier.GetQuerierAPIVersion() |
| 52 | + assert.NoError(t, err) |
| 53 | + |
| 54 | + if unittesting.MaxVersion(cdiVersion, "2.14") != cdiVersion { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + signUpResponse, err := ThirdPartySignInUp("google", "googleID", tplmodels.EmailStruct{ID: "test@example.com"}) |
| 59 | + assert.NoError(t, err) |
| 60 | + |
| 61 | + externalUserId := "externalId" |
| 62 | + externalUserIdInfo := "externalIdInfo" |
| 63 | + createResp, err := supertokens.CreateUserIdMapping(signUpResponse.OK.User.ID, externalUserId, &externalUserIdInfo, false) |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.NotNil(t, createResp.OK) |
| 66 | + |
| 67 | + { // Using supertokens ID |
| 68 | + userResp, err := GetUserByID(signUpResponse.OK.User.ID) |
| 69 | + assert.NoError(t, err) |
| 70 | + assert.Equal(t, externalUserId, userResp.ID) |
| 71 | + } |
| 72 | + |
| 73 | + { // Using external ID |
| 74 | + userResp, err := GetUserByID(externalUserId) |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.Equal(t, externalUserId, userResp.ID) |
| 77 | + } |
| 78 | + |
| 79 | + { // Using thirdparty info |
| 80 | + userResp, err := GetUserByThirdPartyInfo("google", "googleID") |
| 81 | + assert.NoError(t, err) |
| 82 | + assert.Equal(t, externalUserId, userResp.ID) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestPlessCreateUserIdMappingUsingEmail(t *testing.T) { |
| 87 | + BeforeEach() |
| 88 | + unittesting.StartUpST("localhost", "8080") |
| 89 | + defer AfterEach() |
| 90 | + |
| 91 | + initForUserIdMappingTest(t) |
| 92 | + |
| 93 | + querier, err := supertokens.GetNewQuerierInstanceOrThrowError("") |
| 94 | + assert.NoError(t, err) |
| 95 | + |
| 96 | + cdiVersion, err := querier.GetQuerierAPIVersion() |
| 97 | + assert.NoError(t, err) |
| 98 | + |
| 99 | + if unittesting.MaxVersion(cdiVersion, "2.14") != cdiVersion { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + signUpResponse, err := PasswordlessSignInUpByEmail("test@example.com") |
| 104 | + assert.NoError(t, err) |
| 105 | + |
| 106 | + externalUserId := "externalId" |
| 107 | + externalUserIdInfo := "externalIdInfo" |
| 108 | + createResp, err := supertokens.CreateUserIdMapping(signUpResponse.User.ID, externalUserId, &externalUserIdInfo, false) |
| 109 | + assert.NoError(t, err) |
| 110 | + assert.NotNil(t, createResp.OK) |
| 111 | + |
| 112 | + { // Using supertokens ID |
| 113 | + userResp, err := GetUserByID(signUpResponse.User.ID) |
| 114 | + assert.NoError(t, err) |
| 115 | + assert.Equal(t, externalUserId, userResp.ID) |
| 116 | + } |
| 117 | + |
| 118 | + { // Using external ID |
| 119 | + userResp, err := GetUserByID(externalUserId) |
| 120 | + assert.NoError(t, err) |
| 121 | + assert.Equal(t, externalUserId, userResp.ID) |
| 122 | + } |
| 123 | + |
| 124 | + { // Using email |
| 125 | + userResp, err := GetUsersByEmail("test@example.com") |
| 126 | + assert.NoError(t, err) |
| 127 | + assert.NotNil(t, userResp) |
| 128 | + assert.Equal(t, 1, len(userResp)) |
| 129 | + for _, user := range userResp { |
| 130 | + assert.Equal(t, externalUserId, user.ID) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + { // Using sign in |
| 135 | + codeResp, err := CreateCodeWithEmail("test@example.com", nil) |
| 136 | + assert.NoError(t, err) |
| 137 | + assert.NotNil(t, codeResp.OK) |
| 138 | + |
| 139 | + resp, err := ConsumeCodeWithUserInputCode(codeResp.OK.DeviceID, codeResp.OK.UserInputCode, codeResp.OK.PreAuthSessionID) |
| 140 | + assert.NoError(t, err) |
| 141 | + assert.NotNil(t, resp.OK) |
| 142 | + |
| 143 | + assert.Equal(t, externalUserId, resp.OK.User.ID) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestPlessCreateUserIdMappingUsingPhone(t *testing.T) { |
| 148 | + BeforeEach() |
| 149 | + unittesting.StartUpST("localhost", "8080") |
| 150 | + defer AfterEach() |
| 151 | + |
| 152 | + initForUserIdMappingTest(t) |
| 153 | + |
| 154 | + querier, err := supertokens.GetNewQuerierInstanceOrThrowError("") |
| 155 | + assert.NoError(t, err) |
| 156 | + |
| 157 | + cdiVersion, err := querier.GetQuerierAPIVersion() |
| 158 | + assert.NoError(t, err) |
| 159 | + |
| 160 | + if unittesting.MaxVersion(cdiVersion, "2.14") != cdiVersion { |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + signUpResponse, err := PasswordlessSignInUpByPhoneNumber("+919876543210") |
| 165 | + assert.NoError(t, err) |
| 166 | + |
| 167 | + externalUserId := "externalId" |
| 168 | + externalUserIdInfo := "externalIdInfo" |
| 169 | + createResp, err := supertokens.CreateUserIdMapping(signUpResponse.User.ID, externalUserId, &externalUserIdInfo, false) |
| 170 | + assert.NoError(t, err) |
| 171 | + assert.NotNil(t, createResp.OK) |
| 172 | + |
| 173 | + { // Using supertokens ID |
| 174 | + userResp, err := GetUserByID(signUpResponse.User.ID) |
| 175 | + assert.NoError(t, err) |
| 176 | + assert.Equal(t, externalUserId, userResp.ID) |
| 177 | + } |
| 178 | + |
| 179 | + { // Using external ID |
| 180 | + userResp, err := GetUserByID(externalUserId) |
| 181 | + assert.NoError(t, err) |
| 182 | + assert.Equal(t, externalUserId, userResp.ID) |
| 183 | + } |
| 184 | + |
| 185 | + { // Using email |
| 186 | + userResp, err := GetUserByPhoneNumber("+919876543210") |
| 187 | + assert.NoError(t, err) |
| 188 | + assert.Equal(t, externalUserId, userResp.ID) |
| 189 | + } |
| 190 | + |
| 191 | + { // Using sign in |
| 192 | + codeResp, err := CreateCodeWithPhoneNumber("+919876543210", nil) |
| 193 | + assert.NoError(t, err) |
| 194 | + assert.NotNil(t, codeResp.OK) |
| 195 | + |
| 196 | + resp, err := ConsumeCodeWithUserInputCode(codeResp.OK.DeviceID, codeResp.OK.UserInputCode, codeResp.OK.PreAuthSessionID) |
| 197 | + assert.NoError(t, err) |
| 198 | + assert.NotNil(t, resp.OK) |
| 199 | + |
| 200 | + assert.Equal(t, externalUserId, resp.OK.User.ID) |
| 201 | + } |
| 202 | +} |
0 commit comments