Skip to content

Commit 64b421a

Browse files
committed
fix: bulk migration user to role association id handling in case of non primary users
1 parent b1b98c3 commit 64b421a

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

src/main/java/io/supertokens/bulkimport/BulkImport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ private static Map<TenantIdentifier, Map<String, List<String>>> gatherRolesForUs
687687
rolesToUserByTenant.put(tenantIdentifier, new HashMap<>());
688688
}
689689
String userIdToUse = user.externalUserId != null ?
690-
user.externalUserId : user.primaryUserId;
690+
user.externalUserId : user.id;
691691
if(!rolesToUserByTenant.get(tenantIdentifier).containsKey(userIdToUse)){
692692
rolesToUserByTenant.get(tenantIdentifier).put(userIdToUse, new ArrayList<>());
693693
}

src/test/java/io/supertokens/test/bulkimport/apis/ImportUserTest.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,71 @@ public void shouldImportSucceedWithoutAccountLinkingEnabled() throws Exception {
265265
}
266266

267267
FeatureFlagTestContent.getInstance(main).setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES,
268-
new EE_FEATURES[] { EE_FEATURES.MULTI_TENANCY});
268+
new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
269+
270+
BulkImportTestUtils.createTenants(process);
271+
272+
String userJsonStr = """
273+
{
274+
"externalUserId":"some-text-you-like",
275+
"userRoles":[
276+
{
277+
"role":"user",
278+
"tenantIds":[
279+
"public",
280+
"t1"
281+
]
282+
}
283+
],
284+
"loginMethods":[
285+
{
286+
"tenantIds":[
287+
"public",
288+
"t1"
289+
],
290+
"isVerified":true,
291+
"isPrimary":false,
292+
"timeJoinedInMSSinceEpoch":1506523117518,
293+
"recipeId":"emailpassword",
294+
"email":"something0@testing.com",
295+
// passwordHash is randomly generated
296+
"passwordHash":"$argon2id$v=19$m=23298,t=5,p=12$+AlWgiuzC2vqlKrde9G0SG$PmpDeTU2e6ORbHwUMi7MOavS0M3sUJlc9rX/o+nnSxt",
297+
"hashingAlgorithm":"argon2"
298+
}
299+
]
300+
}""";
301+
302+
String user2JsonStr = """
303+
{
304+
"userRoles":[
305+
{
306+
"role":"user",
307+
"tenantIds":[
308+
"public",
309+
"t1"
310+
]
311+
}
312+
],
313+
"loginMethods":[
314+
{
315+
"tenantIds":[
316+
"public",
317+
"t1"
318+
],
319+
"isVerified":true,
320+
"isPrimary":false,
321+
"timeJoinedInMSSinceEpoch":1506523117518,
322+
"recipeId":"emailpassword",
323+
"email":"something1@testing.com",
324+
// passwordHash is randomly generated
325+
"passwordHash":"$argon2id$v=19$m=23298,t=5,p=12$+AlWgiuzC2vqlKrde9G0SG$PmpDeTU2e6ORbHwUMi7MOavS0M3sUJlc9rX/o+nnSxt",
326+
"hashingAlgorithm":"argon2"
327+
}
328+
]
329+
}""";
330+
// Create user roles
331+
UserRoles.createNewRoleOrModifyItsPermissions(main, "user", null);
269332

270-
String userJsonStr = "{\"id\":\"random-id-lol\",\"loginMethods\":[{\"tenantIds\":[\"public\"],\"isVerified\":true,\"isPrimary\":false,\"timeJoinedInMSSinceEpoch\":1741077729471,\"recipeId\":\"emailpassword\",\"email\":\"test@sometestmail.com\",\"passwordHash\":\"$2a\",\"hashingAlgorithm\":\"BCRYPT\"}]}";
271333
JsonObject request = new Gson().fromJson(userJsonStr, JsonObject.class);
272334

273335
JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
@@ -277,6 +339,14 @@ public void shouldImportSucceedWithoutAccountLinkingEnabled() throws Exception {
277339
assertEquals("OK", response.get("status").getAsString());
278340
assertNotNull(response.get("user"));
279341

342+
request = new Gson().fromJson(user2JsonStr, JsonObject.class);
343+
JsonObject response2 = HttpRequestForTesting.sendJsonPOSTRequest(main, "",
344+
"http://localhost:3567/bulk-import/import",
345+
request, 1000, 1000, null, Utils.getCdiVersionStringLatestForTests(), null);
346+
347+
assertEquals("OK", response.get("status").getAsString());
348+
assertNotNull(response.get("user"));
349+
280350
process.kill();
281351
Assert.assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
282352
}

0 commit comments

Comments
 (0)