Skip to content

Commit 7389aaf

Browse files
committed
mirage: Add publish_notifications field
1 parent b80ecc0 commit 7389aaf

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

mirage/factories/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default Factory.extend({
2222
emailVerified: null,
2323
emailVerificationToken: null,
2424
isAdmin: false,
25+
publishNotifications: true,
2526

2627
afterCreate(model) {
2728
if (model.emailVerified === null) {

mirage/route-handlers/users.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,25 @@ export function register(server) {
2323
}
2424

2525
let json = JSON.parse(request.requestBody);
26-
if (!json || !json.user || !('email' in json.user)) {
26+
if (!json || !json.user) {
2727
return new Response(400, {}, { errors: [{ detail: 'invalid json request' }] });
2828
}
29-
if (!json.user.email) {
30-
return new Response(400, {}, { errors: [{ detail: 'empty email rejected' }] });
29+
30+
if (json.user.publish_notifications !== undefined) {
31+
user.update({ publishNotifications: json.user.publish_notifications });
3132
}
3233

33-
user.update({
34-
email: json.user.email,
35-
emailVerified: false,
36-
emailVerificationToken: 'secret123',
37-
});
34+
if (json.user.email !== undefined) {
35+
if (!json.user.email) {
36+
return new Response(400, {}, { errors: [{ detail: 'empty email rejected' }] });
37+
}
38+
39+
user.update({
40+
email: json.user.email,
41+
emailVerified: false,
42+
emailVerificationToken: 'secret123',
43+
});
44+
}
3845

3946
return { ok: true };
4047
});

mirage/serializers/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default BaseSerializer.extend({
2424
delete hash.email;
2525
delete hash.email_verified;
2626
delete hash.is_admin;
27+
delete hash.publish_notifications;
2728
} else {
2829
hash.email_verification_sent = hash.email_verified || Boolean(hash.email_verification_token);
2930
}

tests/mirage/me/get-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module('Mirage | GET /api/v1/me', function (hooks) {
2525
is_admin: false,
2626
login: 'user-1',
2727
name: 'User 1',
28+
publish_notifications: true,
2829
url: 'https://github.com/user-1',
2930
},
3031
owned_crates: [],

tests/mirage/users/update-by-id-test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ module('Mirage | PUT /api/v1/users/:id', function (hooks) {
2424
assert.strictEqual(user.emailVerificationToken, 'secret123');
2525
});
2626

27+
test('updates the user with a new email', async function (assert) {
28+
let user = this.server.create('user');
29+
this.server.create('mirage-session', { user });
30+
assert.true(user.publishNotifications);
31+
32+
let body = JSON.stringify({ user: { publish_notifications: false } });
33+
let response = await fetch(`/api/v1/users/${user.id}`, { method: 'PUT', body });
34+
assert.strictEqual(response.status, 200);
35+
assert.deepEqual(await response.json(), { ok: true });
36+
37+
user.reload();
38+
assert.false(user.publishNotifications);
39+
});
40+
2741
test('returns 403 when not logged in', async function (assert) {
2842
let user = this.server.create('user', { email: 'old@email.com' });
2943

0 commit comments

Comments
 (0)