|
| 1 | +package passwordless |
| 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/supertokens" |
| 9 | + "github.com/supertokens/supertokens-golang/test/unittesting" |
| 10 | +) |
| 11 | + |
| 12 | +func initForUserIdMappingTest(t *testing.T) { |
| 13 | + |
| 14 | + config := supertokens.TypeInput{ |
| 15 | + Supertokens: &supertokens.ConnectionInfo{ |
| 16 | + ConnectionURI: "http://localhost:8080", |
| 17 | + }, |
| 18 | + AppInfo: supertokens.AppInfo{ |
| 19 | + APIDomain: "api.supertokens.io", |
| 20 | + AppName: "SuperTokens", |
| 21 | + WebsiteDomain: "supertokens.io", |
| 22 | + }, |
| 23 | + RecipeList: []supertokens.Recipe{Init(plessmodels.TypeInput{ |
| 24 | + FlowType: "USER_INPUT_CODE_AND_MAGIC_LINK", |
| 25 | + ContactMethodEmailOrPhone: plessmodels.ContactMethodEmailOrPhoneConfig{ |
| 26 | + Enabled: true, |
| 27 | + }, |
| 28 | + })}, |
| 29 | + } |
| 30 | + |
| 31 | + err := supertokens.Init(config) |
| 32 | + assert.NoError(t, err) |
| 33 | +} |
| 34 | + |
| 35 | +func TestCreateUserIdMappingUsingEmail(t *testing.T) { |
| 36 | + BeforeEach() |
| 37 | + unittesting.StartUpST("localhost", "8080") |
| 38 | + defer AfterEach() |
| 39 | + |
| 40 | + initForUserIdMappingTest(t) |
| 41 | + |
| 42 | + querier, err := supertokens.GetNewQuerierInstanceOrThrowError("") |
| 43 | + assert.NoError(t, err) |
| 44 | + |
| 45 | + cdiVersion, err := querier.GetQuerierAPIVersion() |
| 46 | + assert.NoError(t, err) |
| 47 | + |
| 48 | + if unittesting.MaxVersion(cdiVersion, "2.14") != cdiVersion { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + signUpResponse, err := SignInUpByEmail("test@example.com") |
| 53 | + assert.NoError(t, err) |
| 54 | + |
| 55 | + externalUserId := "externalId" |
| 56 | + externalUserIdInfo := "externalIdInfo" |
| 57 | + createResp, err := supertokens.CreateUserIdMapping(signUpResponse.User.ID, externalUserId, &externalUserIdInfo, false) |
| 58 | + assert.NoError(t, err) |
| 59 | + assert.NotNil(t, createResp.OK) |
| 60 | + |
| 61 | + { // Using supertokens ID |
| 62 | + userResp, err := GetUserByID(signUpResponse.User.ID) |
| 63 | + assert.NoError(t, err) |
| 64 | + assert.Equal(t, externalUserId, userResp.ID) |
| 65 | + } |
| 66 | + |
| 67 | + { // Using external ID |
| 68 | + userResp, err := GetUserByID(externalUserId) |
| 69 | + assert.NoError(t, err) |
| 70 | + assert.Equal(t, externalUserId, userResp.ID) |
| 71 | + } |
| 72 | + |
| 73 | + { // Using email |
| 74 | + userResp, err := GetUserByEmail("test@example.com") |
| 75 | + assert.NoError(t, err) |
| 76 | + assert.Equal(t, externalUserId, userResp.ID) |
| 77 | + } |
| 78 | + |
| 79 | + { // Using sign in |
| 80 | + codeResp, err := CreateCodeWithEmail("test@example.com", nil) |
| 81 | + assert.NoError(t, err) |
| 82 | + assert.NotNil(t, codeResp.OK) |
| 83 | + |
| 84 | + resp, err := ConsumeCodeWithUserInputCode(codeResp.OK.DeviceID, codeResp.OK.UserInputCode, codeResp.OK.PreAuthSessionID) |
| 85 | + assert.NoError(t, err) |
| 86 | + assert.NotNil(t, resp.OK) |
| 87 | + |
| 88 | + assert.Equal(t, externalUserId, resp.OK.User.ID) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCreateUserIdMappingUsingPhone(t *testing.T) { |
| 93 | + BeforeEach() |
| 94 | + unittesting.StartUpST("localhost", "8080") |
| 95 | + defer AfterEach() |
| 96 | + |
| 97 | + initForUserIdMappingTest(t) |
| 98 | + |
| 99 | + querier, err := supertokens.GetNewQuerierInstanceOrThrowError("") |
| 100 | + assert.NoError(t, err) |
| 101 | + |
| 102 | + cdiVersion, err := querier.GetQuerierAPIVersion() |
| 103 | + assert.NoError(t, err) |
| 104 | + |
| 105 | + if unittesting.MaxVersion(cdiVersion, "2.14") != cdiVersion { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + signUpResponse, err := SignInUpByPhoneNumber("+919876543210") |
| 110 | + assert.NoError(t, err) |
| 111 | + |
| 112 | + externalUserId := "externalId" |
| 113 | + externalUserIdInfo := "externalIdInfo" |
| 114 | + createResp, err := supertokens.CreateUserIdMapping(signUpResponse.User.ID, externalUserId, &externalUserIdInfo, false) |
| 115 | + assert.NoError(t, err) |
| 116 | + assert.NotNil(t, createResp.OK) |
| 117 | + |
| 118 | + { // Using supertokens ID |
| 119 | + userResp, err := GetUserByID(signUpResponse.User.ID) |
| 120 | + assert.NoError(t, err) |
| 121 | + assert.Equal(t, externalUserId, userResp.ID) |
| 122 | + } |
| 123 | + |
| 124 | + { // Using external ID |
| 125 | + userResp, err := GetUserByID(externalUserId) |
| 126 | + assert.NoError(t, err) |
| 127 | + assert.Equal(t, externalUserId, userResp.ID) |
| 128 | + } |
| 129 | + |
| 130 | + { // Using email |
| 131 | + userResp, err := GetUserByPhoneNumber("+919876543210") |
| 132 | + assert.NoError(t, err) |
| 133 | + assert.Equal(t, externalUserId, userResp.ID) |
| 134 | + } |
| 135 | + |
| 136 | + { // Using sign in |
| 137 | + codeResp, err := CreateCodeWithPhoneNumber("+919876543210", nil) |
| 138 | + assert.NoError(t, err) |
| 139 | + assert.NotNil(t, codeResp.OK) |
| 140 | + |
| 141 | + resp, err := ConsumeCodeWithUserInputCode(codeResp.OK.DeviceID, codeResp.OK.UserInputCode, codeResp.OK.PreAuthSessionID) |
| 142 | + assert.NoError(t, err) |
| 143 | + assert.NotNil(t, resp.OK) |
| 144 | + |
| 145 | + assert.Equal(t, externalUserId, resp.OK.User.ID) |
| 146 | + } |
| 147 | +} |
0 commit comments