File size: 4,997 Bytes
41e9886
 
 
 
 
 
 
45ec8d7
41e9886
 
 
 
 
 
 
45ec8d7
41e9886
 
 
 
afbe0de
41e9886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a4f4cc
 
41e9886
 
 
 
 
 
 
 
 
 
 
 
7bf1507
41e9886
 
 
 
 
 
 
 
 
 
 
 
 
1a4f4cc
 
 
 
 
 
41e9886
 
 
 
 
 
 
 
 
 
 
 
 
1a4f4cc
 
 
 
 
 
41e9886
 
 
 
 
 
 
 
 
 
 
1a4f4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41e9886
1a4f4cc
 
 
 
 
 
41e9886
3199efb
41e9886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a4f4cc
 
 
 
 
 
41e9886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45ec8d7
1a4f4cc
45ec8d7
 
 
41e9886
 
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { assert, it, describe, afterEach, vi, expect } from "vitest";
import type { Cookies } from "@sveltejs/kit";
import { collections } from "$lib/server/database";
import { updateUser } from "./updateUser";
import { ObjectId } from "mongodb";
import { DEFAULT_SETTINGS } from "$lib/types/Settings";
import { defaultModel } from "$lib/server/models";
import { findUser } from "$lib/server/auth";

const userData = {
	preferred_username: "new-username",
	name: "name",
	picture: "https://example.com/avatar.png",
	sub: "1234567890",
};
Object.freeze(userData);

const locals = {
	userId: "1234567890",
	sessionId: "1234567890",
	isAdmin: false,
};

// @ts-expect-error SvelteKit cookies dumb mock
const cookiesMock: Cookies = {
	set: vi.fn(),
};

const insertRandomUser = async () => {
	const res = await collections.users.insertOne({
		_id: new ObjectId(),
		createdAt: new Date(),
		updatedAt: new Date(),
		username: "base-username",
		name: userData.name,
		avatarUrl: userData.picture,
		hfUserId: userData.sub,
		authProvider: "huggingface",
		authId: userData.sub,
	});

	return res.insertedId;
};

const insertRandomConversations = async (count: number) => {
	const res = await collections.conversations.insertMany(
		new Array(count).fill(0).map(() => ({
			_id: new ObjectId(),
			title: "random title",
			messages: [],
			model: defaultModel.id,
			// embedding model removed in this build
			createdAt: new Date(),
			updatedAt: new Date(),
			sessionId: locals.sessionId,
		}))
	);

	return res.insertedIds;
};

describe("login", () => {
	it("should update user if existing", async () => {
		await insertRandomUser();

		await updateUser({
			userData,
			locals,
			cookies: cookiesMock,
			authProvider: "huggingface",
		});

		const existingUser = await collections.users.findOne({ hfUserId: userData.sub });

		assert.equal(existingUser?.name, userData.name);

		expect(cookiesMock.set).toBeCalledTimes(1);
	});

	it("should migrate pre-existing conversations for new user", async () => {
		const insertedId = await insertRandomUser();

		await insertRandomConversations(2);

		await updateUser({
			userData,
			locals,
			cookies: cookiesMock,
			authProvider: "huggingface",
		});

		const conversationCount = await collections.conversations.countDocuments({
			userId: insertedId,
			sessionId: { $exists: false },
		});

		assert.equal(conversationCount, 2);

		await collections.conversations.deleteMany({ userId: insertedId });
	});

	it("stores encrypted hf tokens when provided", async () => {
		await updateUser({
			userData,
			locals,
			cookies: cookiesMock,
			authProvider: "huggingface",
			accessToken: "hf_test_token",
		});

		const user = await collections.users.findOne({ authId: userData.sub });
		assert.exists(user);

		const storedToken = await collections.userTokens.findOne({ userId: user?._id });
		assert.exists(storedToken);
		assert.equal(storedToken?.provider, "huggingface");
	});

	it("does not persist tokens when provider is oidc", async () => {
		await updateUser({
			userData,
			locals,
			cookies: cookiesMock,
			authProvider: "oidc",
			accessToken: "oidc-token",
		});

		const storedToken = await collections.userTokens.findOne({ provider: "huggingface" });
		assert.equal(storedToken, null);
	});

	it("should create default settings for new user", async () => {
		await updateUser({
			userData,
			locals,
			cookies: cookiesMock,
			authProvider: "huggingface",
		});

		const user = (await findUser(locals.sessionId)).user;

		assert.exists(user);

		const settings = await collections.settings.findOne({ userId: user?._id });

		expect(settings).toMatchObject({
			userId: user?._id,
			updatedAt: expect.any(Date),
			createdAt: expect.any(Date),
			...DEFAULT_SETTINGS,
		});

		await collections.settings.deleteOne({ userId: user?._id });
	});

	it("should migrate pre-existing settings for pre-existing user", async () => {
		const { insertedId } = await collections.settings.insertOne({
			sessionId: locals.sessionId,
			updatedAt: new Date(),
			createdAt: new Date(),
			...DEFAULT_SETTINGS,
			shareConversationsWithModelAuthors: false,
		});

		await updateUser({
			userData,
			locals,
			cookies: cookiesMock,
			authProvider: "huggingface",
		});

		const settings = await collections.settings.findOne({
			_id: insertedId,
			sessionId: { $exists: false },
		});

		assert.exists(settings);

		const user = await collections.users.findOne({ hfUserId: userData.sub });

		expect(settings).toMatchObject({
			userId: user?._id,
			updatedAt: expect.any(Date),
			createdAt: expect.any(Date),
			...DEFAULT_SETTINGS,
			shareConversationsWithModelAuthors: false,
		});

		await collections.settings.deleteOne({ userId: user?._id });
	});
});

afterEach(async () => {
	await collections.users.deleteMany({ hfUserId: userData.sub });
	await collections.sessions.deleteMany({});
	await collections.userTokens.deleteMany({});

	locals.userId = "1234567890";
	locals.sessionId = "1234567890";
	vi.clearAllMocks();
});