Skip to content

Commit

Permalink
Merge pull request #174 from parthlambdatest/Dot-3040
Browse files Browse the repository at this point in the history
[Dot-3040] feat: authentication with projectname and creation of project if does not exist
  • Loading branch information
sushobhit-lt authored Nov 28, 2024
2 parents b037475 + b6b9724 commit a41b28e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lambdatest/smartui-cli",
"version": "4.0.11",
"version": "4.0.12",
"description": "A command line interface (CLI) to run SmartUI tests on LambdaTest",
"files": [
"dist/**/*"
Expand Down
6 changes: 4 additions & 2 deletions src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export default (): Env => {
LT_ACCESS_KEY,
LT_SDK_DEBUG,
BASELINE_BRANCH,
CURRENT_BRANCH
CURRENT_BRANCH,
PROJECT_NAME
} = process.env

return {
Expand All @@ -34,6 +35,7 @@ export default (): Env => {
BASELINE_BRANCH,
CURRENT_BRANCH,
LT_SDK_DEBUG: LT_SDK_DEBUG === 'true',
SMARTUI_DO_NOT_USE_CAPTURED_COOKIES: SMARTUI_DO_NOT_USE_CAPTURED_COOKIES === 'true'
SMARTUI_DO_NOT_USE_CAPTURED_COOKIES: SMARTUI_DO_NOT_USE_CAPTURED_COOKIES === 'true',
PROJECT_NAME
}
}
45 changes: 37 additions & 8 deletions src/lib/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@ import pkgJSON from './../../package.json'

export default class httpClient {
axiosInstance: AxiosInstance;
projectToken: string;
projectName: string;
username: string;
accessKey: string;

constructor({ SMARTUI_CLIENT_API_URL, PROJECT_TOKEN, PROJECT_NAME, LT_USERNAME, LT_ACCESS_KEY }: Env) {
this.projectToken = PROJECT_TOKEN || '';
this.projectName = PROJECT_NAME || '';
this.username = LT_USERNAME || '';
this.accessKey = LT_ACCESS_KEY || '';

constructor({ SMARTUI_CLIENT_API_URL, PROJECT_TOKEN }: Env) {
this.axiosInstance = axios.create({
baseURL: SMARTUI_CLIENT_API_URL,
headers: { 'projectToken': PROJECT_TOKEN },
})
});
this.axiosInstance.interceptors.request.use((config) => {
config.headers['projectToken'] = this.projectToken;
config.headers['projectName'] = this.projectName;
config.headers['username'] = this.username;
config.headers['accessKey'] = this.accessKey;
return config;
});
}

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

auth(log: Logger) {
return this.request({
async auth(log: Logger, env: Env): Promise<number> {
let result = 1;
if (this.projectToken) {
result = 0;
}
const response = await this.request({
url: '/token/verify',
method: 'GET'
}, log)
method: 'GET',
}, log);
if (response && response.projectToken) {
this.projectToken = response.projectToken;
env.PROJECT_TOKEN = response.projectToken;
if (response.message && response.message.includes('Project created successfully')) {
result = 2;
}
return result;
} else {
throw new Error('Authentication failed, project token not received');
}
}

createBuild(git: Git, config: any, log: Logger) {
return this.request({
url: '/build',
Expand Down
2 changes: 1 addition & 1 deletion src/lib/snapshotQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default class Queue {

if (!this.ctx.config.delayedUpload && snapshot && snapshot.name && this.snapshotNames.includes(snapshot.name)) {
drop = true;
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.`);
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.`);
}

if (this.ctx.config.delayedUpload && snapshot && snapshot.name && this.snapshotNames.includes(snapshot.name)) {
Expand Down
10 changes: 8 additions & 2 deletions src/tasks/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ export default (ctx: Context): ListrTask<Context, ListrRendererFactory, ListrRen
updateLogContext({task: 'auth'});

try {
await ctx.client.auth(ctx.log);
task.output = chalk.gray(`using project token '******#${ctx.env.PROJECT_TOKEN.split('#').pop()}'`);
const authResult = await ctx.client.auth(ctx.log, ctx.env);
if (authResult === 2) {
task.output = chalk.gray(`New project '${ctx.env.PROJECT_NAME}' created successfully`);
} else if (authResult === 0) {
task.output = chalk.gray(`Using existing project token '******#${ctx.env.PROJECT_TOKEN.split('#').pop()}'`);
} else if (authResult === 1) {
task.output = chalk.gray(`Using existing project '${ctx.env.PROJECT_NAME}'`);
}
task.title = 'Authenticated with SmartUI';
} catch (error: any) {
ctx.log.debug(error);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface Env {
LT_SDK_DEBUG: boolean;
BASELINE_BRANCH: string | undefined;
CURRENT_BRANCH: string | undefined;
PROJECT_NAME: string | undefined;
}

export interface Snapshot {
Expand Down

0 comments on commit a41b28e

Please sign in to comment.