Skip to content

Commit

Permalink
Added mountebank
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 4, 2024
1 parent 2e04942 commit da3b08b
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/backend/src/FoodDiary.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public void ConfigureServices(IServiceCollection services)
})
.AddGoogle(Constants.AuthenticationSchemes.OAuthGoogle, options =>
{
options.AuthorizationEndpoint = _googleAuthOptions.AuthorizationEndpoint;
options.TokenEndpoint = _googleAuthOptions.TokenEndpoint;
options.UserInformationEndpoint = _googleAuthOptions.UserInformationEndpoint;
options.SignInScheme = Constants.AuthenticationSchemes.Cookie;
options.ClientId = _googleAuthOptions.ClientId;
options.ClientSecret = _googleAuthOptions.ClientSecret;
Expand Down
5 changes: 4 additions & 1 deletion src/backend/src/FoodDiary.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
},
"GoogleAuth": {
"ClientId": "772368064111-19hqh3c6ksu56ke45nm24etn7qoma88v.apps.googleusercontent.com",
"ClientSecret": ""
"ClientSecret": "",
"AuthorizationEndpoint": "https://accounts.google.com/o/oauth2/v2/auth",
"TokenEndpoint": "https://oauth2.googleapis.com/token",
"UserInformationEndpoint": "https://www.googleapis.com/oauth2/v2/userinfo"
},
"Import": {
"MaxImportFileLengthBytes": 5242880
Expand Down
7 changes: 5 additions & 2 deletions src/backend/src/FoodDiary.Configuration/GoogleAuthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ namespace FoodDiary.Configuration;

public class GoogleAuthOptions
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string ClientId { get; init; }
public string ClientSecret { get; init; }
public string AuthorizationEndpoint { get; init; }
public string TokenEndpoint { get; init; }
public string UserInformationEndpoint { get; init; }
}
218 changes: 203 additions & 15 deletions tests/.pnp.cjs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ services:
test: ["CMD-SHELL", "pg_isready -p 8090 -U postgres"]
interval: 10s
retries: 5
mountebank:
image: bbyars/mountebank:2.9.1
ports:
- "2125:2525"
- "2126:2126"
web:
depends_on:
db:
Expand All @@ -31,6 +36,7 @@ services:
- Google__ExportFolderId=test_folder_id
- GoogleAuth__ClientId=test_client_id
- GoogleAuth__ClientSecret=test_client_secret
- GoogleAuth__AuthorizationEndpoint=http://localhost:2126/authorize
volumes:
- ../certs:/https:ro
entrypoint: >
Expand Down
43 changes: 43 additions & 0 deletions tests/fakeAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Mountebank,
Imposter,
HttpMethod,
Stub,
EqualPredicate,
Response,
} from '@anev/ts-mountebank';

const PORT = 2126;

const HTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
Hello world!
</body>
</html>
`;

export class FakeAuthProvider {
private readonly _mountebank = new Mountebank().withURL('http://localhost:2125');
private readonly _imposter = new Imposter().withPort(PORT);

public async setup(): Promise<void> {
const authorizeStub = new Stub()
.withPredicate(new EqualPredicate().withMethod(HttpMethod.GET).withPath('/authorize'))
.withResponse(
new Response().withStatusCode(200).withHeader('Content-Type', 'text/html').withBody(HTML),
);

await this._mountebank.createImposter(this._imposter.withStub(authorizeStub));
}

public async teardown(): Promise<void> {
await this._mountebank.deleteImposter(PORT);
}
}
1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"lint:fix": "eslint . --fix"
},
"devDependencies": {
"@anev/ts-mountebank": "^1.8.0",
"@playwright/test": "^1.40.1",
"@types/eslint": "^8",
"@types/node": "^18.16.0",
Expand Down
18 changes: 16 additions & 2 deletions tests/scenarios/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { test, expect } from '@playwright/test';
import { FakeAuthProvider } from '../fakeAuth';

let fakeAuth: FakeAuthProvider | null = null;

test.beforeEach(async () => {
fakeAuth = new FakeAuthProvider();
await fakeAuth.setup();
});

test.afterEach(async () => {
await fakeAuth?.teardown();
});

test('should display sign in page', async ({ page }) => {
await page.goto('https://localhost:10000');
await page.goto('https://localhost:8080');

await page.getByRole('button', { name: /sign in with google/i }).click();

await expect(page.getByRole('button', { name: /sign in with google/i })).toBeVisible();
await expect(page.getByText(/hello world/i)).toBeVisible();
});
Loading

0 comments on commit da3b08b

Please sign in to comment.