Skip to content

Commit

Permalink
fix: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Jul 8, 2024
1 parent a7241ec commit 9de5e82
Show file tree
Hide file tree
Showing 2 changed files with 787 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/test/java/io/supertokens/test/TelemetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import io.supertokens.cronjobs.telemetry.Telemetry;
import io.supertokens.dashboard.Dashboard;
import io.supertokens.httpRequest.HttpRequestMocking;
import io.supertokens.multitenancy.Multitenancy;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager.TestingProcess;
import io.supertokens.version.Version;
Expand Down Expand Up @@ -187,6 +189,89 @@ protected URLConnection openConnection(URL u) {
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STOPPED));
}

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

TestingProcess process = TestingProcessManager.start(args, false);
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getBaseStorage(process.getProcess()).getType() == STORAGE_TYPE.SQL) {
Dashboard.signUpDashboardUser(process.getProcess(), "[email protected]", "password123");
}

Multitenancy.saveWebsiteAndAPIDomainForApp(StorageLayer.getBaseStorage(process.getProcess()),
new AppIdentifier(null, null), "https://example.com", "https://api.example.com");

// Restarting the process to send telemetry again
process.kill(false);
process = TestingProcessManager.start(args, false);

ByteArrayOutputStream output = new ByteArrayOutputStream();
final HttpURLConnection mockCon = mock(HttpURLConnection.class);
InputStream inputStrm = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
when(mockCon.getInputStream()).thenReturn(inputStrm);
when(mockCon.getResponseCode()).thenReturn(200);
when(mockCon.getOutputStream()).thenReturn(new OutputStream() {
@Override
public void write(int b) {
output.write(b);
}
});

HttpRequestMocking.getInstance(process.getProcess()).setMockURL(Telemetry.REQUEST_ID,
new HttpRequestMocking.URLGetter() {

@Override
public URL getUrl(String url) throws MalformedURLException {
URLStreamHandler stubURLStreamHandler = new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) {
return mockCon;
}
};
return new URL(null, url, stubURLStreamHandler);
}
});

process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (Version.getVersion(process.getProcess()).getPluginName().equals("sqlite")) {
return;
}

assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.SENT_TELEMETRY));

JsonObject telemetryData = new JsonParser().parse(output.toString()).getAsJsonObject();
assertEquals(9, telemetryData.entrySet().size());

assertTrue(telemetryData.has("telemetryId"));
assertEquals(telemetryData.get("superTokensVersion").getAsString(),
Version.getVersion(process.getProcess()).getCoreVersion());
assertEquals(telemetryData.get("appId").getAsString(), "public");
assertEquals(telemetryData.get("connectionUriDomain").getAsString(), "");
assertTrue(telemetryData.has("maus"));
assertTrue(telemetryData.has("dashboardUserEmails"));
assertEquals("https://example.com", telemetryData.get("websiteDomain").getAsString());
assertEquals("https://api.example.com", telemetryData.get("apiDomain").getAsString());

if (StorageLayer.getBaseStorage(process.getProcess()).getType() == STORAGE_TYPE.SQL) {
assertEquals(1, telemetryData.get("dashboardUserEmails").getAsJsonArray().size());
assertEquals("[email protected]", telemetryData.get("dashboardUserEmails").getAsJsonArray().get(0).getAsString());
assertEquals(31, telemetryData.get("maus").getAsJsonArray().size());
assertEquals(0, telemetryData.get("usersCount").getAsInt());
} else {
assertEquals(0, telemetryData.get("dashboardUserEmails").getAsJsonArray().size());
assertEquals(0, telemetryData.get("maus").getAsJsonArray().size());
assertEquals(-1, telemetryData.get("usersCount").getAsInt());
}

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

@Test
public void testThatTelemetryIdDoesNotChange() throws Exception {
String telemetryId = null;
Expand Down
Loading

0 comments on commit 9de5e82

Please sign in to comment.