@zebrunner/javascript-agent-mocha
Version:
Zebrunner Agent for Mocha
268 lines (216 loc) • 8.6 kB
JavaScript
const ConfigResolver = require('./config-resolver');
const {
HttpClient,
jsonHeaders,
multipartDataHeaders,
buildMultiPartStream,
} = require('./api-client-axios');
const {
API_URLS,
getRefreshToken,
getTestRunStart,
getTestRunEnd,
getTestStart,
getTestEnd,
getAttachLabels,
getAttachArtifactReferences,
setMaintainer,
getUpsertTestCases,
getUpdateTcmConfigs,
} = require('./request-builder');
const { isEmptyObject, generateMultipartBoundary } = require('./utils');
class ZebrunnerApiClient {
#reporterConfig;
#configResolver;
#httpClient;
#accessToken;
constructor(config) {
this.#reporterConfig = config;
this.#configResolver = new ConfigResolver(this.#reporterConfig);
this.#httpClient = new HttpClient(this.#configResolver);
this.#accessToken = null;
}
#refreshToken = async () => {
if (!this.#accessToken) {
const res = await this.#httpClient.callPost(
API_URLS.URL_REFRESH,
getRefreshToken(this.#configResolver.getReportingServerAccessToken()),
jsonHeaders.headers,
);
this.#accessToken = `${res.data.authTokenType} ${res.data.authToken}`;
}
return this.#accessToken;
};
#getHeadersWithAuth = async (basicHeaders) => {
const authToken = await this.#refreshToken();
if (authToken) {
const authHeaders = basicHeaders.headers;
authHeaders.Authorization = authToken;
return authHeaders;
}
};
#initializeRunContext = async () => {
const runContext = this.#configResolver.getReportingRunContext();
if (runContext) {
const headers = await this.#getHeadersWithAuth(jsonHeaders);
const { data } = await this.#httpClient.callPost(
API_URLS.URL_EXCHANGE_RUN_CONTEXT,
runContext,
headers,
);
if (!data.runAllowed) {
throw new Error(`Zebrunner: Reporting is not allowed. Reason: ${data.reason}`);
}
return data.testRunUuid;
}
};
#startTestRun = async (testRunUuid) => {
const projectKey = this.#configResolver.getReportingProjectKey();
const url = API_URLS.URL_START_RUN.replace('${project}', projectKey);
const testRunStartBody = getTestRunStart(this.#reporterConfig, testRunUuid);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
const { data } = await this.#httpClient.callPost(url, testRunStartBody, headers);
return data.id;
};
startLaunch = async () => {
const testRunUuid = await this.#initializeRunContext();
return this.#startTestRun(testRunUuid);
};
finishLaunch = async (testRunId) => {
const url = API_URLS.URL_FINISH_RUN.concat(testRunId);
const testRunEndBody = getTestRunEnd();
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPut(url, testRunEndBody, headers);
};
startTest = async (testRunId, test) => {
const url = API_URLS.URL_START_TEST.replace('${testRunId}', testRunId);
const testStartBody = getTestStart(test);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPost(url, testStartBody, headers);
};
finishTest = async (testRunId, testId, test) => {
const url = API_URLS.URL_FINISH_TEST.replace('${testRunId}', testRunId).replace(
'${testId}',
testId,
);
const testEndBody = getTestEnd(test);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPut(url, testEndBody, headers);
};
attachTestRunLabels = async (testRunId, labels) => {
if (labels && labels.length !== 0) {
const url = API_URLS.URL_ATTACH_TEST_RUN_LABELS.replace('${testRunId}', testRunId);
const attachLabelsBody = getAttachLabels(labels);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPut(url, attachLabelsBody, headers);
}
};
attachTestRunArtifactReferences = async (testRunId, references) => {
if (references && references.length !== 0) {
const url = API_URLS.URL_ATTACH_TEST_RUN_ARTIFACT_REFERENCES.replace(
'${testRunId}',
testRunId,
);
const attachReferencesBody = getAttachArtifactReferences(references);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPut(url, attachReferencesBody, headers);
}
};
uploadTestRunArtifact = async (testRunId, file) => {
if (file) {
const url = API_URLS.URL_UPLOAD_TEST_RUN_ARTIFACT.replace('${testRunId}', testRunId);
const boundary = generateMultipartBoundary();
const attachArtifactMultipartBody = buildMultiPartStream(file.name, file, boundary);
const headers = await this.#getHeadersWithAuth(multipartDataHeaders(boundary));
return this.#httpClient.callPost(url, attachArtifactMultipartBody, headers);
}
};
saveTcmConfigs = async (testRunId) => {
const updateTcmConfigsBody = getUpdateTcmConfigs(this.#reporterConfig);
if (!isEmptyObject(updateTcmConfigsBody)) {
const url = API_URLS.URL_UPDATE_TCM_CONFIGS.replace('${testRunId}', testRunId);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPatch(url, updateTcmConfigsBody, headers);
}
};
attachTestLabels = async (testRunId, testId, labels) => {
if (testId && labels && labels.length !== 0) {
const url = API_URLS.URL_ATTACH_TEST_LABELS.replace('${testRunId}', testRunId).replace(
'${testId}',
testId,
);
const attachLabelsBody = getAttachLabels(labels);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPut(url, attachLabelsBody, headers);
}
};
attachTestArtifactReferences = async (testRunId, testId, references) => {
if (testId && references && references.length !== 0) {
const url = API_URLS.URL_ATTACH_TEST_ARTIFACT_REFERENCES.replace(
'${testRunId}',
testRunId,
).replace('${testId}', testId);
const attachReferencesBody = getAttachArtifactReferences(references);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPut(url, attachReferencesBody, headers);
}
};
uploadTestArtifact = async (testRunId, testId, file) => {
if (testId && file) {
const url = API_URLS.URL_UPLOAD_TEST_ARTIFACT.replace('${testRunId}', testRunId).replace(
'${testId}',
testId,
);
const boundary = generateMultipartBoundary();
const attachArtifactMultipartBody = buildMultiPartStream(file.name, file, boundary);
const headers = await this.#getHeadersWithAuth(multipartDataHeaders(boundary));
return this.#httpClient.callPost(url, attachArtifactMultipartBody, headers);
}
};
setTestMaintainer = async (testRunId, testId, maintainer) => {
if (testId && maintainer) {
const url = API_URLS.URL_UPDATE_TEST.replace('${testRunId}', testRunId).replace(
'${testId}',
testId,
);
const setMaintainerBody = setMaintainer(maintainer);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPatch(url, setMaintainerBody, headers);
}
};
revertTestRegistration = async (testRunId, testId) => {
if (testId) {
const url = API_URLS.URL_REVERT_TEST_REGISTRATION.replace('${testRunId}', testRunId).replace(
'${testId}',
testId,
);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callDelete(url, headers);
}
};
upsertTestTestCases = async (testRunId, testId, testCases) => {
if (testId) {
const url = API_URLS.URL_UPSERT_TEST_TEST_CASES.replace('${testRunId}', testRunId).replace(
'${testId}',
testId,
);
const upsertTestCasesBody = getUpsertTestCases(testCases);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPost(url, upsertTestCasesBody, headers);
}
};
sendLogs = async (testRunId, testId, logMessages) => {
if (testId && logMessages && logMessages.length !== 0) {
const logsBody = logMessages.map((log) => ({
testId,
message: log.message,
level: log.level,
timestamp: Date.now(),
}));
const url = API_URLS.URL_SEND_LOGS.replace('${testRunId}', testRunId);
const headers = await this.#getHeadersWithAuth(jsonHeaders);
return this.#httpClient.callPost(url, logsBody, headers);
}
};
}
module.exports = ZebrunnerApiClient;