Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show custom alert message for duplicate email address #912

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/components/user/edit/basic-details.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ except according to the terms contained in the LICENSE file.

<script setup>
import { computed, inject, ref } from 'vue';
import { equals } from 'ramda';
import { useI18n } from 'vue-i18n';

import FormGroup from '../../form-group.vue';
Expand Down Expand Up @@ -70,7 +71,11 @@ const submit = () => {
user.email = data.email;
user.displayName = data.displayName;
user.updatedAt = data.updatedAt;
}
},
problemToAlert: ({ code, details }) =>
(code === 409.3 && equals(details.fields, ['email', 'deleted'])
? t('problem.409_3', { email: details.values[0] })
: null)
})
.then(() => { alert.success(t('alert.success')); })
.catch(noop);
Expand All @@ -86,6 +91,9 @@ const submit = () => {
"action": {
"update": "Update details"
},
"problem": {
"409_3": "You cannot change your email to {email} because this account already exists. Please try another email address."
},
"alert": {
"success": "User details saved!"
}
Expand Down
17 changes: 15 additions & 2 deletions src/components/user/new.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ except according to the terms contained in the LICENSE file.
</template>

<script>
import { equals } from 'ramda';

import FormGroup from '../form-group.vue';
import Modal from '../modal.vue';
import Spinner from '../spinner.vue';
Expand Down Expand Up @@ -80,7 +82,15 @@ export default {
submit() {
const postData = { email: this.email };
if (this.displayName !== '') postData.displayName = this.displayName;
this.request({ method: 'POST', url: '/v1/users', data: postData })
this.request({
method: 'POST',
url: '/v1/users',
data: postData,
problemToAlert: ({ code, details }) =>
(code === 409.3 && equals(details.fields, ['email', 'deleted'])
? this.$t('problem.409_3', { email: details.values[0] })
: null)
})
.then(response => {
this.$emit('success', response.data);
})
Expand All @@ -100,7 +110,10 @@ export default {
],
"oidcIntroduction": [
"Users on your login server must have a Central account to log in to Central. Once you create this account, the user on your login server with the email address you provide will be able to log in to Central."
]
],
"problem": {
"409_3": "It looks like {email} already has an account. Please try another email address."
}
}
}
</i18n>
Expand Down
45 changes: 45 additions & 0 deletions test/components/user/edit/basic-details.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,51 @@ describe('UserEditBasicDetails', () => {
});
});

describe('custom alert messages', () => {
beforeEach(() => {
mockLogin({ email: 'old@email.com' });
});

it('shows a custom message for a duplicate email', () =>
mockHttp()
.mount(UserEditBasicDetails, mountOptions())
.request(async (component) => {
await component.get('input[type="email"]').setValue('new@email.com');
return component.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with email,deleted value(s) of new@email.com,false.',
details: {
fields: ['email', 'deleted'],
values: ['new@email.com', false]
}
})
.afterResponse(modal => {
modal.should.alert('danger', (message) => {
message.should.startWith('You cannot change your email to new@email.com');
});
}));

// I don't think a different uniqueness violation is currently possible.
// This is mostly about future-proofing.
it('shows the default message for a different uniqueness violation', () =>
mockHttp()
.mount(UserEditBasicDetails, mountOptions())
.request(async (component) => {
await component.get('input[type="email"]').setValue('new@email.com');
return component.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with foo value(s) of bar.',
details: { fields: ['foo'], values: ['bar'] }
})
.afterResponse(modal => {
modal.should.alert('danger', 'A resource already exists with foo value(s) of bar.');
}));
});

describe('after a successful response', () => {
beforeEach(() => {
mockLogin({ displayName: 'Old Name' });
Expand Down
57 changes: 51 additions & 6 deletions test/components/user/new.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ describe('UserNew', () => {
await modal.get('input[type="email"]').setValue('new@email.com');
return modal.get('form').trigger('submit');
})
.beforeEachResponse((_, { method, url, data }) => {
method.should.equal('POST');
url.should.equal('/v1/users');
data.should.eql({ email: 'new@email.com' });
})
.respondWithProblem());
.respondWithProblem()
.testRequests([{
method: 'POST',
url: '/v1/users',
data: { email: 'new@email.com' }
}]));

it('sends the display name if there is one', () =>
mockHttp()
Expand Down Expand Up @@ -107,6 +107,51 @@ describe('UserNew', () => {
modal: true
}));

describe('custom alert messages', () => {
it('shows a custom message for a duplicate email', () =>
mockHttp()
.mount(UserNew, {
props: { state: true }
})
.request(async (modal) => {
await modal.get('input[type="email"]').setValue('new@email.com');
return modal.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with email,deleted value(s) of new@email.com,false.',
details: {
fields: ['email', 'deleted'],
values: ['new@email.com', false]
}
})
.afterResponse(modal => {
modal.should.alert('danger', (message) => {
message.should.startWith('It looks like new@email.com already has an account.');
});
}));

// I don't think a different uniqueness violation is currently possible.
// This is mostly about future-proofing.
it('shows the default message for a different uniqueness violation', () =>
mockHttp()
.mount(UserNew, {
props: { state: true }
})
.request(async (modal) => {
await modal.get('input[type="email"]').setValue('new@email.com');
return modal.get('form').trigger('submit');
})
.respondWithProblem({
code: 409.3,
message: 'A resource already exists with foo value(s) of bar.',
details: { fields: ['foo'], values: ['bar'] }
})
.afterResponse(modal => {
modal.should.alert('danger', 'A resource already exists with foo value(s) of bar.');
}));
});

describe('after a successful response', () => {
const submitWithSuccess = () => load('/users', { root: false })
.complete()
Expand Down
10 changes: 10 additions & 0 deletions transifex/strings_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4280,6 +4280,11 @@
"developer_comment": "This is the text for an action, for example, the text of a button."
}
},
"problem": {
"409_3": {
"string": "You cannot change your email to {email} because this account already exists. Please try another email address."
}
},
"alert": {
"success": {
"string": "User details saved!"
Expand Down Expand Up @@ -4366,6 +4371,11 @@
"0": {
"string": "Users on your login server must have a Central account to log in to Central. Once you create this account, the user on your login server with the email address you provide will be able to log in to Central."
}
},
"problem": {
"409_3": {
"string": "It looks like {email} already has an account. Please try another email address."
}
}
},
"UserResetPassword": {
Expand Down