Skip to content

Commit b05ebda

Browse files
committed
start setting up sandbox again.
1 parent 742fe63 commit b05ebda

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

config/dev.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ config :bike_brigade, BikeBrigade.Repo,
1010
database: "bike_brigade_dev",
1111
hostname: "localhost",
1212
show_sensitive_data_on_connection_error: true,
13-
pool_size: 10
13+
pool_size: 10,
14+
pool: Ecto.Adapters.SQL.Sandbox
1415

1516
# For development, we disable any cache and enable
1617
# debugging and code reloading.
@@ -66,6 +67,7 @@ config :bike_brigade, BikeBrigadeWeb.Endpoint,
6667

6768
# Do not include metadata nor timestamps in development logs
6869
config :logger, :console, format: "[$level] $message\n"
70+
# config :logger, level: :warning
6971

7072
# Disable the extremely annoying debug logging for the spreadsheet library
7173
config :logger,

lib/bike_brigade_web/endpoint.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
defmodule BikeBrigadeWeb.Endpoint do
22
use Phoenix.Endpoint, otp_app: :bike_brigade
3+
require Logger
4+
35

46
if sandbox = Application.compile_env(:bike_brigade, :sandbox) do
57
plug Phoenix.Ecto.SQL.Sandbox, sandbox: sandbox
@@ -21,6 +23,24 @@ defmodule BikeBrigadeWeb.Endpoint do
2123

2224
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
2325

26+
27+
defp log_sandbox_requests(conn, _opts) do
28+
if conn.request_path == "/sandbox" do
29+
Logger.info("2@@@@@@@@@@@@@@@@@@@@@ SANDBOX REQUEST: #{conn.method} #{conn.request_path}")
30+
end
31+
conn
32+
end
33+
34+
plug :log_sandbox_requests
35+
36+
plug Phoenix.Ecto.SQL.Sandbox,
37+
at: "/sandbox",
38+
header: "x-session-id",
39+
repo: BikeBrigade.Repo,
40+
timeout: 15_000 # the default
41+
42+
43+
2444
# Serve at "/static" the static files from "priv/static" directory.
2545
#
2646
# You should set gzip to true if you are running phx.digest

test/e2e/all.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { test, expect, Page } from '@playwright/test';
1+
import { Page } from '@playwright/test';
2+
import { test, expect } from "./helpers/sandbox";
23
import { faker } from '@faker-js/faker';
34

45
const programName = faker.company.name()

test/e2e/helpers/sandbox.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// tests/helpers/sandbox.js
2+
import { request, test as base } from '@playwright/test';
3+
4+
async function setupSandbox(context: any) {
5+
// Create sandbox
6+
const requestContext = await request.newContext();
7+
const response = await requestContext.post('http://localhost:4000/sandbox', {
8+
headers: {
9+
'Cache-Control': 'no-store'
10+
}
11+
});
12+
13+
const sessionId = await response.text();
14+
15+
// Set up the route interception to add sessionId to all requests
16+
await context.route('**/*', async (route: any, request: any) => {
17+
const headers = request.headers();
18+
headers['x-session-id'] = sessionId;
19+
await route.continue({ headers });
20+
});
21+
22+
// Store the sessionId for LiveView connections
23+
await context.addInitScript(({ sessionId }) => {
24+
window.sessionId = sessionId;
25+
}, { sessionId });
26+
27+
return sessionId;
28+
}
29+
30+
async function teardownSandbox(sessionId: any) {
31+
const requestContext = await request.newContext();
32+
await requestContext.delete('http://localhost:4000/sandbox', {
33+
headers: {
34+
'x-session-id': sessionId
35+
}
36+
});
37+
}
38+
39+
// module.exports = { setupSandbox, teardownSandbox };
40+
41+
42+
43+
const test = base.extend({
44+
context: async ({ context }, use) => {
45+
const sessionId = await setupSandbox(context);
46+
console.log("sessionId is >>>>>>>>>>: ", sessionId)
47+
await use(context);
48+
await teardownSandbox(sessionId);
49+
}
50+
});
51+
52+
const expect = base.expect
53+
54+
export {
55+
test,
56+
expect
57+
}

0 commit comments

Comments
 (0)