Skip to content

Commit

Permalink
Fix broken e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
irby committed Jul 5, 2023
1 parent 9d2bd0e commit fb380a7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
21 changes: 11 additions & 10 deletions e2e/tests/example.spec.ts → e2e/tests/secrets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ test('Submitting secret results in secret creation response', async ({page}) =>
const successMessage = await page.getByText('Secret received successfully. Use the link below to access the secret:');
const secretUrl = await page.inputValue('input#secretUrl');

await expect(successMessage).toBeTruthy();
await expect(secretUrl).toBeTruthy();
await expect(successMessage).toBeVisible();
await expect(secretUrl).toContain(environment.frontendUrl);
});

test('Secret can be retrieved after it is created', async ({page}) => {
const secretMessage = "Major Tom to ground control";
const secretMessage = "Ground Control to Major Tom";
await page.goto(environment.frontendUrl);
const textBox = await page.locator('//*[@id="secretText"]');
const submitButton = await page.locator('//*[@id="submit"]');
Expand All @@ -33,7 +33,7 @@ test('Secret can be retrieved after it is created', async ({page}) => {
});

test('Accessing the secret twice results in error', async ({page}) => {
const secretMessage = "Major Tom to ground control";
const secretMessage = "Your circuit's dead, is something wrong?";
await page.goto(environment.frontendUrl);
const textBox = await page.locator('//*[@id="secretText"]');
const submitButton = await page.locator('//*[@id="submit"]');
Expand All @@ -44,13 +44,14 @@ test('Accessing the secret twice results in error', async ({page}) => {

await page.goto(secretUrl);

const secretRetrieve1 = await page.locator('//*[@id="secretMessage"]');
await expect(await secretRetrieve1.count()).toBe(1);
const secretRetrieve1 = await page.getByText(secretMessage);
await expect(secretRetrieve1).toBeVisible();

await page.reload();
await page.goto(secretUrl);

const secretRetrieve2 = await page.locator('//*[@id="secretMessage"]');
await expect(await secretRetrieve2.count()).toBe(0);
const errorMessage = await page.getByText("Either the secret not found, has expired, or has already been recovered – or your key was invalid.");
const secretRetrieve2 = await page.getByText(secretMessage);

await expect(await page.getByText("Either the secret not found, has expired, or has already been recovered – or your key was invalid.")).toBeTruthy();
await expect(errorMessage).toBeVisible();
await expect(secretRetrieve2).not.toBeVisible();
});
3 changes: 2 additions & 1 deletion src/spa/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod
RUN npm run build

FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=node /app/dist/spa /usr/share/nginx/html
EXPOSE 80
12 changes: 12 additions & 0 deletions src/spa/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server {
listen 80;

root /usr/share/nginx/html;
index index.html;

server_name _;

location / {
try_files $uri /index.html;
}
}
42 changes: 41 additions & 1 deletion src/spa/src/app/create/create.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,45 @@ describe('CreateComponent', () => {

expect(component.expiryTimeInMinutes).toBe(100);
});
})
});

describe('convertDateToString', () => {
it('calling with date returns formatted string', () => {
const dateTime = new Date("2023-07-05T05:06:03.4532984+00:00");
const expected = "7/5/2023 05:06:03";
const actual = component.convertDateToString(dateTime);

expect(actual).toBe(expected);
});
});

describe('padTimeUnit', () => {
it('pads if value is less than 10 (low)', () => {
const expected = "00";
const actual = component.padTimeUnit(0);

expect(actual).toBe(expected);
});

it('pads if value is less than 10 (high)', () => {
const expected = "09";
const actual = component.padTimeUnit(9);

expect(actual).toBe(expected);
});

it('does not pad if value is equal to 10', () => {
const expected = "10";
const actual = component.padTimeUnit(10);

expect(actual).toBe(expected);
});

it('does not pad if value is greater than 10', () => {
const expected = "11";
const actual = component.padTimeUnit(11);

expect(actual).toBe(expected);
});
});
});
21 changes: 21 additions & 0 deletions src/spa/src/app/create/create.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export class CreateComponent implements OnInit {
const response = await axios.post<SecretSubmissionResponse>(environment.apiUrl + '/api/secrets',
new SecretSubmissionRequest(this.secretText.value, this.expiryTimeInMinutes));
this.secretCreationResponse = response.data;
const expiry = new Date(this.secretCreationResponse.expireDateTime);
this.expireDateTime = this.convertDateToString(expiry);
}
catch (error: any) {
const err = error as AxiosError<HttpErrorResponse>;
Expand Down Expand Up @@ -141,4 +143,23 @@ export class CreateComponent implements OnInit {
this.expiryTimeInMinutes = value;
}

convertDateToString(date: Date): string {
const month = date.getUTCMonth()+1;
const day = date.getUTCDate();
const year = date.getUTCFullYear();
const hours = this.padTimeUnit(date.getUTCHours());
const minutes = this.padTimeUnit(date.getUTCMinutes());
const seconds = this.padTimeUnit(date.getUTCSeconds());
return `${month}/${day}/${year} ${hours}:${minutes}:${seconds}`;
}

padTimeUnit(value: number): string {
let result = '';
if (value < 10) {
result += '0';
}
result += value;
return result;
}

}

0 comments on commit fb380a7

Please sign in to comment.