aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/test/functions/sessions.test.js
blob: 107e321904d070ac2916976b4418f9b4b6d46bf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// @ts-check
import assert from 'node:assert/strict';
import { mkdir, rm } from 'node:fs/promises';
import { after, before, describe, it } from 'node:test';
import { BlobsServer } from '@netlify/blobs/server';
import * as devalue from 'devalue';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import netlify from '../../dist/index.js';
const token = 'mock';
const siteID = '1';
const dataDir = '.netlify/sessions';
const options = {
	name: 'test',
	uncachedEdgeURL: `http://localhost:8971`,
	edgeURL: `http://localhost:8971`,
	token,
	siteID,
	region: 'us-east-1',
};

describe('Astro.session', () => {
	describe('Production', () => {
		/** @type {import('../../../../astro/test/test-utils.js').Fixture} */
		let fixture;

		/** @type {BlobsServer} */
		let blobServer;
		before(async () => {
			process.env.NETLIFY = '1';
			await rm(dataDir, { recursive: true, force: true }).catch(() => {});
			await mkdir(dataDir, { recursive: true });
			blobServer = new BlobsServer({
				directory: dataDir,
				token,
				port: 8971,
			});
			await blobServer.start();
			fixture = await loadFixture({
				// @ts-ignore
				root: new URL('./fixtures/sessions/', import.meta.url),
				output: 'server',
				adapter: netlify(),
				// @ts-ignore
				session: { driver: '', options },
			});
			await fixture.build({});
			const entryURL = new URL(
				'./fixtures/sessions/.netlify/v1/functions/ssr/ssr.mjs',
				import.meta.url,
			);
			const mod = await import(entryURL.href);
			handler = mod.default;
		});
		let handler;
		after(async () => {
			await blobServer.stop();
			delete process.env.NETLIFY;
		});
		async function fetchResponse(path, requestInit) {
			return handler(new Request(new URL(path, 'http://example.com'), requestInit), {});
		}

		it('can regenerate session cookies upon request', async () => {
			const firstResponse = await fetchResponse('/regenerate', { method: 'GET' });
			const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
			const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

			const secondResponse = await fetchResponse('/regenerate', {
				method: 'GET',
				headers: {
					cookie: `astro-session=${firstSessionId}`,
				},
			});
			const secondHeaders = secondResponse.headers.get('set-cookie').split(',');
			const secondSessionId = secondHeaders[0].split(';')[0].split('=')[1];
			assert.notEqual(firstSessionId, secondSessionId);
		});

		it('can save session data by value', async () => {
			const firstResponse = await fetchResponse('/update', { method: 'GET' });
			const firstValue = await firstResponse.json();
			assert.equal(firstValue.previousValue, 'none');

			const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
			const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];
			const secondResponse = await fetchResponse('/update', {
				method: 'GET',
				headers: {
					cookie: `astro-session=${firstSessionId}`,
				},
			});
			const secondValue = await secondResponse.json();
			assert.equal(secondValue.previousValue, 'expected');
		});

		it('can save and restore URLs in session data', async () => {
			const firstResponse = await fetchResponse('/_actions/addUrl', {
				method: 'POST',
				headers: {
					'Content-Type': 'application/json',
				},
				body: JSON.stringify({ favoriteUrl: 'https://domain.invalid' }),
			});

			assert.equal(firstResponse.ok, true);
			const firstHeaders = firstResponse.headers.get('set-cookie').split(',');
			const firstSessionId = firstHeaders[0].split(';')[0].split('=')[1];

			const data = devalue.parse(await firstResponse.text());
			assert.equal(data.message, 'Favorite URL set to https://domain.invalid/ from nothing');
			const secondResponse = await fetchResponse('/_actions/addUrl', {
				method: 'POST',
				headers: {
					'Content-Type': 'application/json',
					cookie: `astro-session=${firstSessionId}`,
				},
				body: JSON.stringify({ favoriteUrl: 'https://example.com' }),
			});
			const secondData = devalue.parse(await secondResponse.text());
			assert.equal(
				secondData.message,
				'Favorite URL set to https://example.com/ from https://domain.invalid/',
			);
		});
	});
});