Skip to content

Commit 05475fc

Browse files
committed
Refactor test file names and import paths
1 parent 8dc8d68 commit 05475fc

File tree

6 files changed

+104
-100
lines changed

6 files changed

+104
-100
lines changed
File renamed without changes.

tests/utils/data-creator.cjs tests/utils/data/data-generators.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
const { generateKey } = require('./crypto.cjs');
2+
const { generateKey } = require('../crypto/crypto.cjs');
33

44
/**
55
* Date formats.

tests/utils/fixtures.cjs

-20
This file was deleted.

tests/utils/helpers.cjs

-79
This file was deleted.

tests/utils/http/http-utils.cjs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
function createRequestOptions(method, body) {
4+
return {
5+
method,
6+
body: body ? JSON.stringify(body) : undefined,
7+
headers: {
8+
'Content-Type': 'application/json',
9+
},
10+
};
11+
}
12+
13+
function createAuthorizedRequestOptions(method, body, token) {
14+
const options = createRequestOptions(method, body);
15+
options.headers.Authorization = `Token ${token}`;
16+
return options;
17+
}
18+
19+
module.exports = {
20+
createRequestOptions,
21+
createAuthorizedRequestOptions,
22+
};

tests/utils/user/user-utils.cjs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
const fs = require('node:fs');
4+
const path = require('node:path');
5+
const { request } = require('undici');
6+
const {
7+
randomEmail,
8+
randomPassword,
9+
randomUsername,
10+
} = require('../data/data-generators.cjs');
11+
const { createRequestOptions } = require('../http/http-utils.cjs');
12+
const { env } = require('../env.cjs');
13+
14+
const $ConduitAPI = env.conduitAPI;
15+
16+
// Load test user data
17+
const testSettingsPath = path.join(__dirname, '../../../test-settings.json');
18+
const testSettings = JSON.parse(fs.readFileSync(testSettingsPath, 'utf8'));
19+
const testUserData = testSettings['test-user'];
20+
21+
async function ensureTestUserExists() {
22+
try {
23+
await loginTestUser();
24+
} catch (error) {
25+
if (error.message.includes('Failed to log in user')) {
26+
await createTestUser();
27+
} else {
28+
throw error;
29+
}
30+
}
31+
}
32+
33+
function createTestUserData() {
34+
return {
35+
user: {
36+
email: randomEmail(),
37+
password: randomPassword(),
38+
username: randomUsername(),
39+
},
40+
};
41+
}
42+
43+
async function createTestUser() {
44+
try {
45+
const response = await request(
46+
`${$ConduitAPI}/users`,
47+
createRequestOptions('POST', { user: testUserData })
48+
);
49+
const responseBody = await response.body.json();
50+
if (response.statusCode !== 201) {
51+
throw new Error(`User creation failed: ${JSON.stringify(responseBody)}`);
52+
}
53+
return { userData: testUserData, userResponse: responseBody.user };
54+
} catch (error) {
55+
throw new Error(`createTestUser error: ${error.message}`);
56+
}
57+
}
58+
59+
async function loginTestUser(userData = testUserData) {
60+
try {
61+
const response = await request(
62+
`${$ConduitAPI}/users/login`,
63+
createRequestOptions('POST', { user: userData })
64+
);
65+
const responseBody = await response.body.json();
66+
if (response.statusCode !== 200) {
67+
throw new Error(`Failed to log in user: ${JSON.stringify(responseBody)}`);
68+
}
69+
return responseBody.user.token;
70+
} catch (error) {
71+
throw new Error(`loginTestUser error: ${error.message}`);
72+
}
73+
}
74+
75+
module.exports = {
76+
getTestUserData: () => ({ user: testUserData }),
77+
ensureTestUserExists,
78+
createTestUser,
79+
loginTestUser,
80+
createTestUserData,
81+
};

0 commit comments

Comments
 (0)