/home/techb158/cosmic.abdallabala.com/tests
Edit: /home/techb158/cosmic.abdallabala.com/tests/oauth-live-connectors.test.js (5622B)
const assert = require("assert");
const fs = require("fs");
const path = require("path");
const os = require("os");
const { JsonDatabase } = require("../src/storage/jsonDatabase.js");
const { OAuthService } = require("../src/services/oauthService.js");
const { IntegrationService } = require("../src/services/integrationService.js");
const { buildAuthorizationUrl, getCredentialStatus } = require("../src/integrations/oauthProviderConfig.js");
const { encryptToken, decryptToken, redactToken } = require("../src/security/tokenVault.js");
const { createLivePmClient } = require("../src/integrations/livePmClientFactory.js");
const source = path.join(__dirname, "..", "data", "database.json");
const temp = path.join(os.tmpdir(), `cosmic-oauth-db-${Date.now()}.json`);
fs.copyFileSync(source, temp);
const env = {
COSMIC_PUBLIC_BASE_URL: "http://localhost:8090",
COSMIC_TOKEN_ENCRYPTION_KEY: "test-only-encryption-secret",
TRELLO_API_KEY: "trello-key",
TRELLO_TOKEN: "trello-token",
TRELLO_LIST_ID: "trello-list-id",
JIRA_CLIENT_ID: "jira-client-id",
JIRA_CLIENT_SECRET: "jira-client-secret",
JIRA_ACCESS_TOKEN: "jira-oauth-token",
JIRA_CLOUD_ID: "jira-cloud-id",
JIRA_PROJECT_KEY: "AIRISK",
ASANA_CLIENT_ID: "asana-client-id",
ASANA_CLIENT_SECRET: "asana-client-secret",
ASANA_ACCESS_TOKEN: "asana-token",
ASANA_PROJECT_GID: "1200000000000001",
MS_CLIENT_ID: "ms-client-id",
MS_CLIENT_SECRET: "ms-client-secret",
MS_TENANT_ID: "organizations",
MICROSOFT_GRAPH_ACCESS_TOKEN: "ms-graph-token",
PLANNER_PLAN_ID: "planner-plan-id",
PLANNER_BUCKET_ID: "planner-bucket-id"
};
const encrypted = encryptToken("secret-token", env);
assert.notStrictEqual(encrypted, "secret-token", "Token vault should encrypt token values");
assert.strictEqual(decryptToken(encrypted, env), "secret-token", "Token vault should decrypt token values");
assert.ok(redactToken("abcdef123456").includes("***"), "Token redaction should mask sensitive values");
const jiraUrl = buildAuthorizationUrl("Jira", "state-123", env);
assert.ok(jiraUrl.startsWith("https://auth.atlassian.com/authorize"), "Jira should use Atlassian OAuth authorize endpoint");
assert.ok(jiraUrl.includes("audience=api.atlassian.com"), "Jira authorization URL should include Atlassian audience");
const asanaUrl = buildAuthorizationUrl("Asana", "state-123", env);
assert.ok(asanaUrl.startsWith("https://app.asana.com/-/oauth_authorize"), "Asana should use Asana OAuth authorize endpoint");
const plannerUrl = buildAuthorizationUrl("Microsoft Planner", "state-123", env);
assert.ok(plannerUrl.startsWith("https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize"), "Planner should use Microsoft identity authorize endpoint");
const trelloUrl = buildAuthorizationUrl("Trello", "state-123", env);
assert.ok(trelloUrl.startsWith("https://trello.com/1/authorize"), "Trello should use Trello authorization endpoint");
assert.strictEqual(getCredentialStatus("Jira", env).configured, true, "Jira OAuth credentials should be detected");
const database = new JsonDatabase(temp);
const oauthService = new OAuthService(database, { env });
const integrationService = new IntegrationService(database, { oauthService, env });
const providers = oauthService.listProviders();
assert.strictEqual(providers.length, 4, "OAuth provider list should expose four providers");
assert.ok(providers.every(item => item.configured), "All test providers should be configured");
const integrations = integrationService.listIntegrations("cosmic-ai-risk-001");
const trelloIntegration = integrations.find(item => item.provider === "Trello");
const authPayload = oauthService.buildAuthorizePayload("Trello", trelloIntegration.id, "USER-OAUTH");
assert.ok(authPayload.authorizeUrl.includes("trello.com/1/authorize"), "OAuth service should create Trello authorization payload");
const stored = oauthService.storeTrelloToken("stored-trello-token", authPayload.state);
assert.strictEqual(stored.provider, "Trello", "Trello token should be stored with provider metadata");
assert.strictEqual(oauthService.hasStoredToken("Trello", trelloIntegration.id), true, "Stored token should be available by integration");
const liveStatus = integrationService.getLiveStatus(trelloIntegration.id);
assert.strictEqual(liveStatus.credentials.configured, true, "Live status should include credential status");
assert.ok(liveStatus.configurationHints.length, "Live status should include provider setup hints");
const dryRunTest = integrationService.testLiveIntegration(trelloIntegration.id);
Promise.resolve(dryRunTest).then(result => {
assert.strictEqual(result.dryRun, true, "Live test should dry-run unless COSMIC_LIVE_PM_ENABLED=true");
let calledUrl = "";
const fakeFetch = async (url, options) => {
calledUrl = url;
return {
ok: true,
status: 200,
text: async () => JSON.stringify({ id: "me", username: "cosmic-test", fullName: "COSMIC Test" })
};
};
const client = createLivePmClient("Trello", { id: trelloIntegration.id, provider: "Trello", external_project_key: "trello-list-id" }, oauthService, { env, fetchImpl: fakeFetch });
return client.testConnection().then(connection => {
assert.strictEqual(connection.ok, true, "Trello live client should parse connection response");
assert.ok(calledUrl.includes("api.trello.com/1/members/me"), "Trello client should call members/me endpoint");
fs.unlinkSync(temp);
console.log("All COSMIC AI-Risk OAuth and live connector tests passed.");
});
}).catch(error => {
if (fs.existsSync(temp)) fs.unlinkSync(temp);
console.error(error);
process.exit(1);
});