Skip to content

Commit a41b28e

Browse files
authored
Merge pull request #174 from parthlambdatest/Dot-3040
[Dot-3040] feat: authentication with projectname and creation of project if does not exist
2 parents b037475 + b6b9724 commit a41b28e

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lambdatest/smartui-cli",
3-
"version": "4.0.11",
3+
"version": "4.0.12",
44
"description": "A command line interface (CLI) to run SmartUI tests on LambdaTest",
55
"files": [
66
"dist/**/*"

src/lib/env.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default (): Env => {
1616
LT_ACCESS_KEY,
1717
LT_SDK_DEBUG,
1818
BASELINE_BRANCH,
19-
CURRENT_BRANCH
19+
CURRENT_BRANCH,
20+
PROJECT_NAME
2021
} = process.env
2122

2223
return {
@@ -34,6 +35,7 @@ export default (): Env => {
3435
BASELINE_BRANCH,
3536
CURRENT_BRANCH,
3637
LT_SDK_DEBUG: LT_SDK_DEBUG === 'true',
37-
SMARTUI_DO_NOT_USE_CAPTURED_COOKIES: SMARTUI_DO_NOT_USE_CAPTURED_COOKIES === 'true'
38+
SMARTUI_DO_NOT_USE_CAPTURED_COOKIES: SMARTUI_DO_NOT_USE_CAPTURED_COOKIES === 'true',
39+
PROJECT_NAME
3840
}
3941
}

src/lib/httpClient.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,27 @@ import pkgJSON from './../../package.json'
88

99
export default class httpClient {
1010
axiosInstance: AxiosInstance;
11+
projectToken: string;
12+
projectName: string;
13+
username: string;
14+
accessKey: string;
15+
16+
constructor({ SMARTUI_CLIENT_API_URL, PROJECT_TOKEN, PROJECT_NAME, LT_USERNAME, LT_ACCESS_KEY }: Env) {
17+
this.projectToken = PROJECT_TOKEN || '';
18+
this.projectName = PROJECT_NAME || '';
19+
this.username = LT_USERNAME || '';
20+
this.accessKey = LT_ACCESS_KEY || '';
1121

12-
constructor({ SMARTUI_CLIENT_API_URL, PROJECT_TOKEN }: Env) {
1322
this.axiosInstance = axios.create({
1423
baseURL: SMARTUI_CLIENT_API_URL,
15-
headers: { 'projectToken': PROJECT_TOKEN },
16-
})
24+
});
25+
this.axiosInstance.interceptors.request.use((config) => {
26+
config.headers['projectToken'] = this.projectToken;
27+
config.headers['projectName'] = this.projectName;
28+
config.headers['username'] = this.username;
29+
config.headers['accessKey'] = this.accessKey;
30+
return config;
31+
});
1732
}
1833

1934
async request(config: AxiosRequestConfig, log: Logger): Promise<Record<string, any>> {
@@ -46,13 +61,27 @@ export default class httpClient {
4661
})
4762
}
4863

49-
auth(log: Logger) {
50-
return this.request({
64+
async auth(log: Logger, env: Env): Promise<number> {
65+
let result = 1;
66+
if (this.projectToken) {
67+
result = 0;
68+
}
69+
const response = await this.request({
5170
url: '/token/verify',
52-
method: 'GET'
53-
}, log)
71+
method: 'GET',
72+
}, log);
73+
if (response && response.projectToken) {
74+
this.projectToken = response.projectToken;
75+
env.PROJECT_TOKEN = response.projectToken;
76+
if (response.message && response.message.includes('Project created successfully')) {
77+
result = 2;
78+
}
79+
return result;
80+
} else {
81+
throw new Error('Authentication failed, project token not received');
82+
}
5483
}
55-
84+
5685
createBuild(git: Git, config: any, log: Logger) {
5786
return this.request({
5887
url: '/build',

src/lib/snapshotQueue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export default class Queue {
271271

272272
if (!this.ctx.config.delayedUpload && snapshot && snapshot.name && this.snapshotNames.includes(snapshot.name)) {
273273
drop = true;
274-
this.ctx.log.info(`Skipping duplicate SmartUI snapshot '${snapshot.name}'. To capture duplicate screenshots, please set the 'delayedUploads' configuration as true in your config file.`);
274+
this.ctx.log.info(`Skipping duplicate SmartUI snapshot '${snapshot.name}'. To capture duplicate screenshots, please set the 'delayedUpload' configuration as true in your config file.`);
275275
}
276276

277277
if (this.ctx.config.delayedUpload && snapshot && snapshot.name && this.snapshotNames.includes(snapshot.name)) {

src/tasks/auth.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
1010
updateLogContext({task: 'auth'});
1111

1212
try {
13-
await ctx.client.auth(ctx.log);
14-
task.output = chalk.gray(`using project token '******#${ctx.env.PROJECT_TOKEN.split('#').pop()}'`);
13+
const authResult = await ctx.client.auth(ctx.log, ctx.env);
14+
if (authResult === 2) {
15+
task.output = chalk.gray(`New project '${ctx.env.PROJECT_NAME}' created successfully`);
16+
} else if (authResult === 0) {
17+
task.output = chalk.gray(`Using existing project token '******#${ctx.env.PROJECT_TOKEN.split('#').pop()}'`);
18+
} else if (authResult === 1) {
19+
task.output = chalk.gray(`Using existing project '${ctx.env.PROJECT_NAME}'`);
20+
}
1521
task.title = 'Authenticated with SmartUI';
1622
} catch (error: any) {
1723
ctx.log.debug(error);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface Env {
6969
LT_SDK_DEBUG: boolean;
7070
BASELINE_BRANCH: string | undefined;
7171
CURRENT_BRANCH: string | undefined;
72+
PROJECT_NAME: string | undefined;
7273
}
7374

7475
export interface Snapshot {

0 commit comments

Comments
 (0)