Skip to content

Commit

Permalink
fix: active users data saved into public tenant storage
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Nov 1, 2023
1 parent edb4c2e commit 504c8e3
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/main/java/io/supertokens/webserver/WebserverAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.emailpassword.exceptions.UnknownUserIdException;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifierWithStorage;
Expand Down Expand Up @@ -340,6 +341,16 @@ protected AppIdentifierWithStorage getAppIdentifierWithStorageFromRequestAndEnfo
storage, storages);
}

protected AppIdentifierWithStorage getPublicTenantStorage(HttpServletRequest req)
throws ServletException, TenantOrAppNotFoundException {
AppIdentifier appIdentifier = new AppIdentifier(this.getConnectionUriDomain(req), this.getAppId(req));

Storage storage = StorageLayer.getStorage(appIdentifier.getAsPublicTenantIdentifier(), main);

return appIdentifier.withStorage(storage);

}

protected TenantIdentifierWithStorageAndUserIdMapping getTenantIdentifierWithStorageAndUserIdMappingFromRequest(
HttpServletRequest req, String userId, UserIdType userIdType)
throws StorageQueryException, TenantOrAppNotFoundException, UnknownUserIdException, ServletException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
password);
io.supertokens.useridmapping.UserIdMapping.populateExternalUserIdForUsers(tenantIdentifierWithStorage, new AuthRecipeUserInfo[]{user});

ActiveUsers.updateLastActive(tenantIdentifierWithStorage.toAppIdentifierWithStorage(), main,
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main,
user.getSupertokensUserId()); // use the internal user id

JsonObject result = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
TenantIdentifierWithStorage tenant = this.getTenantIdentifierWithStorageFromRequest(req);
AuthRecipeUserInfo user = EmailPassword.signUp(tenant, super.main, normalisedEmail, password);

ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main, user.getSupertokensUserId());
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main, user.getSupertokensUserId());

JsonObject result = new JsonObject();
result.addProperty("status", "OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
getVersionFromRequest(req).greaterThanOrEqualTo(SemVer.v4_0));
io.supertokens.useridmapping.UserIdMapping.populateExternalUserIdForUsers(this.getTenantIdentifierWithStorageFromRequest(req), new AuthRecipeUserInfo[]{consumeCodeResponse.user});

ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main, consumeCodeResponse.user.getSupertokensUserId());
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main, consumeCodeResponse.user.getSupertokensUserId());

JsonObject result = new JsonObject();
result.addProperty("status", "OK");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
this.getAppIdentifierWithStorage(req),
sessionInfo.session.userId, UserIdType.ANY);
if (userIdMapping != null) {
ActiveUsers.updateLastActive(appIdentifierWithStorage, main,
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main,
userIdMapping.superTokensUserId);
} else {
ActiveUsers.updateLastActive(appIdentifierWithStorage, main,
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main,
sessionInfo.session.userId);
}
} catch (StorageQueryException ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
this.getAppIdentifierWithStorage(req),
sessionInfo.session.userId, UserIdType.ANY);
if (userIdMapping != null) {
ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main,
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main,
userIdMapping.superTokensUserId);
} else {
ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main,
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main,
sessionInfo.session.userId);
}
} catch (StorageQueryException ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
this.getAppIdentifierWithStorage(req),
userId, UserIdType.ANY);
if (userIdMapping != null) {
ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main,
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main,
userIdMapping.superTokensUserId);
} else {
ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main, userId);
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main, userId);
}
} catch (StorageQueryException ignored) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
thirdPartyUserId, email, isEmailVerified);
UserIdMapping.populateExternalUserIdForUsers(this.getTenantIdentifierWithStorageFromRequest(req), new AuthRecipeUserInfo[]{response.user});

ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main, response.user.getSupertokensUserId());
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main, response.user.getSupertokensUserId());

JsonObject result = new JsonObject();
result.addProperty("status", "OK");
Expand Down Expand Up @@ -140,7 +140,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
email, isEmailVerified);
UserIdMapping.populateExternalUserIdForUsers(this.getTenantIdentifierWithStorageFromRequest(req), new AuthRecipeUserInfo[]{response.user});

ActiveUsers.updateLastActive(this.getAppIdentifierWithStorage(req), main, response.user.getSupertokensUserId());
ActiveUsers.updateLastActive(this.getPublicTenantStorage(req), main, response.user.getSupertokensUserId());

JsonObject result = new JsonObject();
result.addProperty("status", "OK");
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/io/supertokens/test/ActiveUsersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import io.supertokens.ActiveUsers;
import io.supertokens.Main;
import io.supertokens.ProcessState;
import io.supertokens.featureflag.EE_FEATURES;
import io.supertokens.featureflag.FeatureFlagTestContent;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.httpRequest.HttpRequestForTesting;
import io.supertokens.test.httpRequest.HttpResponseException;
import io.supertokens.test.multitenant.api.TestMultitenancyAPIHelper;
import io.supertokens.utils.SemVer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -212,4 +217,80 @@ public void activeUserCountAPITest() throws Exception {
assert res.get("count").getAsInt() == 2;
}

@Test
public void testThatActiveUserDataIsSavedInPublicTenantStorage() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
FeatureFlagTestContent.getInstance(process.getProcess())
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.MULTI_TENANCY});
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

{ // Create a tenant
JsonObject coreConfig = new JsonObject();

StorageLayer.getStorage(new TenantIdentifier(null, null, null), process.getProcess())
.modifyConfigToAddANewUserPoolForTesting(coreConfig, 1);

TestMultitenancyAPIHelper.createTenant(
process.getProcess(),
new TenantIdentifier(null, null, null),
"t1", true, true, true,
coreConfig);
}

{ // no active users yet
HashMap<String, String> params = new HashMap<>();
params.put("since", "0");
JsonObject res = HttpRequestForTesting.sendGETRequest(
process.getProcess(),
"",
"http://localhost:3567/users/count/active",
params,
1000,
1000,
null,
Utils.getCdiVersionStringLatestForTests(),
"");

assert res.get("status").getAsString().equals("OK");
assert res.get("count").getAsInt() == 0;
}

{ // Sign up, which updates active users
JsonObject responseBody = new JsonObject();
responseBody.addProperty("email", "[email protected]");
responseBody.addProperty("password", "validPass123");

JsonObject signInResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
"http://localhost:3567/t1/recipe/signup", responseBody, 1000, 1000, null, SemVer.v4_0.get(),
"emailpassword");
}

{ // 1 active user in the public tenant
HashMap<String, String> params = new HashMap<>();
params.put("since", "0");
JsonObject res = HttpRequestForTesting.sendGETRequest(
process.getProcess(),
"",
"http://localhost:3567/users/count/active",
params,
1000,
1000,
null,
Utils.getCdiVersionStringLatestForTests(),
"");

assert res.get("status").getAsString().equals("OK");
assert res.get("count").getAsInt() == 1;
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}

0 comments on commit 504c8e3

Please sign in to comment.