Andrew commited on
Commit
1a4f4cc
·
1 Parent(s): 64950db

test(auth): update user tests for multi-provider auth

Browse files
src/routes/login/callback/updateUser.spec.ts CHANGED
@@ -35,6 +35,8 @@ const insertRandomUser = async () => {
35
  name: userData.name,
36
  avatarUrl: userData.picture,
37
  hfUserId: userData.sub,
 
 
38
  });
39
 
40
  return res.insertedId;
@@ -61,7 +63,12 @@ describe("login", () => {
61
  it("should update user if existing", async () => {
62
  await insertRandomUser();
63
 
64
- await updateUser({ userData, locals, cookies: cookiesMock });
 
 
 
 
 
65
 
66
  const existingUser = await collections.users.findOne({ hfUserId: userData.sub });
67
 
@@ -75,7 +82,12 @@ describe("login", () => {
75
 
76
  await insertRandomConversations(2);
77
 
78
- await updateUser({ userData, locals, cookies: cookiesMock });
 
 
 
 
 
79
 
80
  const conversationCount = await collections.conversations.countDocuments({
81
  userId: insertedId,
@@ -87,8 +99,43 @@ describe("login", () => {
87
  await collections.conversations.deleteMany({ userId: insertedId });
88
  });
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  it("should create default settings for new user", async () => {
91
- await updateUser({ userData, locals, cookies: cookiesMock });
 
 
 
 
 
92
 
93
  const user = (await findUser(locals.sessionId)).user;
94
 
@@ -115,7 +162,12 @@ describe("login", () => {
115
  shareConversationsWithModelAuthors: false,
116
  });
117
 
118
- await updateUser({ userData, locals, cookies: cookiesMock });
 
 
 
 
 
119
 
120
  const settings = await collections.settings.findOne({
121
  _id: insertedId,
@@ -141,6 +193,7 @@ describe("login", () => {
141
  afterEach(async () => {
142
  await collections.users.deleteMany({ hfUserId: userData.sub });
143
  await collections.sessions.deleteMany({});
 
144
 
145
  locals.userId = "1234567890";
146
  locals.sessionId = "1234567890";
 
35
  name: userData.name,
36
  avatarUrl: userData.picture,
37
  hfUserId: userData.sub,
38
+ authProvider: "huggingface",
39
+ authId: userData.sub,
40
  });
41
 
42
  return res.insertedId;
 
63
  it("should update user if existing", async () => {
64
  await insertRandomUser();
65
 
66
+ await updateUser({
67
+ userData,
68
+ locals,
69
+ cookies: cookiesMock,
70
+ authProvider: "huggingface",
71
+ });
72
 
73
  const existingUser = await collections.users.findOne({ hfUserId: userData.sub });
74
 
 
82
 
83
  await insertRandomConversations(2);
84
 
85
+ await updateUser({
86
+ userData,
87
+ locals,
88
+ cookies: cookiesMock,
89
+ authProvider: "huggingface",
90
+ });
91
 
92
  const conversationCount = await collections.conversations.countDocuments({
93
  userId: insertedId,
 
99
  await collections.conversations.deleteMany({ userId: insertedId });
100
  });
101
 
102
+ it("stores encrypted hf tokens when provided", async () => {
103
+ await updateUser({
104
+ userData,
105
+ locals,
106
+ cookies: cookiesMock,
107
+ authProvider: "huggingface",
108
+ accessToken: "hf_test_token",
109
+ });
110
+
111
+ const user = await collections.users.findOne({ authId: userData.sub });
112
+ assert.exists(user);
113
+
114
+ const storedToken = await collections.userTokens.findOne({ userId: user?._id });
115
+ assert.exists(storedToken);
116
+ assert.equal(storedToken?.provider, "huggingface");
117
+ });
118
+
119
+ it("does not persist tokens when provider is oidc", async () => {
120
+ await updateUser({
121
+ userData,
122
+ locals,
123
+ cookies: cookiesMock,
124
+ authProvider: "oidc",
125
+ accessToken: "oidc-token",
126
+ });
127
+
128
+ const storedToken = await collections.userTokens.findOne({ provider: "huggingface" });
129
+ assert.equal(storedToken, null);
130
+ });
131
+
132
  it("should create default settings for new user", async () => {
133
+ await updateUser({
134
+ userData,
135
+ locals,
136
+ cookies: cookiesMock,
137
+ authProvider: "huggingface",
138
+ });
139
 
140
  const user = (await findUser(locals.sessionId)).user;
141
 
 
162
  shareConversationsWithModelAuthors: false,
163
  });
164
 
165
+ await updateUser({
166
+ userData,
167
+ locals,
168
+ cookies: cookiesMock,
169
+ authProvider: "huggingface",
170
+ });
171
 
172
  const settings = await collections.settings.findOne({
173
  _id: insertedId,
 
193
  afterEach(async () => {
194
  await collections.users.deleteMany({ hfUserId: userData.sub });
195
  await collections.sessions.deleteMany({});
196
+ await collections.userTokens.deleteMany({});
197
 
198
  locals.userId = "1234567890";
199
  locals.sessionId = "1234567890";