Skip to content

Commit

Permalink
Merge pull request #410 from webkom/renameLegoUser
Browse files Browse the repository at this point in the history
Rename legoUser to identifier so it's not linked to LEGO
  • Loading branch information
LudvigHz authored Feb 7, 2021
2 parents f1a7094 + 34e741c commit 2c4ebd4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 38 deletions.
16 changes: 8 additions & 8 deletions app/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ exports.create = (req, res) => {
};

exports.generate = async (req, res) => {
const { legoUser, email, ignoreExistingUser } = req.body;
const { identifier, email, ignoreExistingUser } = req.body;

if (!legoUser) throw new errors.InvalidPayloadError('legoUser');
if (!identifier) throw new errors.InvalidPayloadError('identifier');
if (!email) throw new errors.InvalidPayloadError('email');

// Try to fetch an entry from the register with this username
const entry = await Register.findOne({ legoUser }).exec();
const entry = await Register.findOne({ identifier }).exec();

if (entry && ignoreExistingUser) {
return res.status(409).json(legoUser);
return res.status(409).json(identifier);
}

// Entry has no user this user is allready activated
Expand All @@ -63,7 +63,7 @@ exports.generate = async (req, res) => {
.then(() =>
res.status(409).json({
status: 'allready signed in',
user: legoUser,
user: identifier,
})
)
.catch((err) => {
Expand All @@ -86,7 +86,7 @@ exports.generate = async (req, res) => {
.then(() =>
res.status(201).json({
status: 'regenerated',
user: legoUser,
user: identifier,
})
)
.catch((err) => {
Expand All @@ -109,9 +109,9 @@ exports.generate = async (req, res) => {
return User.register(user, password)
.then((createdUser) =>
mailHandler('send', { email, username: createdUser.username, password })
.then(() => new Register({ legoUser, email, user }).save())
.then(() => new Register({ identifier, email, user }).save())
.then(() =>
res.status(201).json({ status: 'generated', user: legoUser })
res.status(201).json({ status: 'generated', user: identifier })
)
.catch((err) => {
throw new errors.MailError(err);
Expand Down
8 changes: 4 additions & 4 deletions app/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,16 @@ class DuplicateUsernameError extends Error {

exports.DuplicateUsernameError = DuplicateUsernameError;

class DuplicateLegoUserError extends Error {
class DuplicateIdentifierError extends Error {
constructor() {
super();
this.name = 'DuplicateLegoUserError';
this.message = 'This LEGO user has allready gotten a user.';
this.name = 'DuplicateIdentifierError';
this.message = 'This identifier has allready gotten a user.';
this.status = 409;
}
}

exports.DuplicateLegoUserError = DuplicateLegoUserError;
exports.DuplicateIdentifierError = DuplicateIdentifierError;

class AlreadyActiveElectionError extends Error {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion app/models/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const registerSchema = new Schema({
legoUser: {
identifier: {
type: String,
required: true,
unique: true,
Expand Down
8 changes: 4 additions & 4 deletions app/views/partials/moderator/generateUser.pug
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.col-xs-12.col-sm-offset-3.col-sm-6.col-md-offset-4.col-md-4.text-center
form.form-group(ng-submit='generateUser(user)', name='generateUserForm')
.form-group
label LEGO User
label Identifikator
input.form-control(
type='text',
name='legoUser',
placeholder='Skriv inn LEGO User',
ng-model='user.legoUser'
name='identifier',
placeholder='Skriv inn identifikator',
ng-model='user.identifier'
)

.form-group
Expand Down
6 changes: 3 additions & 3 deletions app/views/partials/moderator/manageRegister.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
| laget med "Registrer bruker" eller "QR" vil ikke vises under.
.center(style='margin-top: 50px')
hr
.usage-flex
.usage-flex(style='margin: 0')
h4(style='text-align:right') Totalt {{ registers.length }} genererte brukere
label Search:
input(ng-model='searchText')

table(style='width:100%; margin-top: 30px')
thead(style='border-bottom:2px solid black')
tr
th Brukernavn
th Identifikator
th Email
th(style='text-align:center') Fullført registrering
tbody(style='font-size:18px')
tr(ng-repeat='register in registers | filter:searchText')
th {{ register.legoUser }}
th {{ register.identifier }}
th {{ register.email }}
th(style='text-align:center') {{ register.user ? "Nei" : "Ja" }}
th(style='text-align:right')
Expand Down
2 changes: 1 addition & 1 deletion client/controllers/generateUserCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = [
switch (response.status) {
case 409:
alertService.addError(
'Denne LEGO brukeren har allerede fått en bruker.'
'Denne idenfikatoren har allerede fått en bruker.'
);
break;
default:
Expand Down
8 changes: 4 additions & 4 deletions deployment/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ func runLegoToVoteSync(interrupt chan os.Signal, url *url.URL, jwt string) error
}

voteFormData := struct {
Email string `json:"email"`
LegoUser string `json:"legoUser"`
Email string `json:"email"`
Identifier string `json:"identifier"`
}{
Email: legoUserData.Email,
LegoUser: action.Payload.User.Username,
Email: legoUserData.Email,
Identifier: action.Payload.User.Username,
}

out, err := json.Marshal(voteFormData)
Expand Down
4 changes: 2 additions & 2 deletions test/api/register.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Register API', () => {
passportStub.login(this.moderatorUser.username);
await request(app)
.post('/api/user/generate')
.send({ legoUser: 'username', email: 'email@domain.com' });
.send({ identifier: 'username', email: 'email@domain.com' });
});

after(() => {
Expand All @@ -34,7 +34,7 @@ describe('Register API', () => {
.expect(200)
.expect('Content-Type', /json/);
body.length.should.equal(1);
body[0].legoUser.should.equal('username');
body[0].identifier.should.equal('username');
body[0].email.should.equal('email@domain.com');
});

Expand Down
26 changes: 15 additions & 11 deletions test/api/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('User API', () => {
};

const genUserData = {
legoUser: 'legoUsername',
identifier: 'identifiername',
email: 'test@user.com',
};

Expand Down Expand Up @@ -376,7 +376,7 @@ describe('User API', () => {
.expect(201)
.expect('Content-Type', /json/);
body.status.should.equal('generated');
body.user.should.equal(genUserData.legoUser);
body.user.should.equal(genUserData.identifier);
});

it('should be not be possible to generate a user for a user', async function () {
Expand All @@ -390,7 +390,7 @@ describe('User API', () => {
error.status.should.equal(403);
});

it('should get an error when generating user with no legoUser', async function () {
it('should get an error when generating user with no identifier', async function () {
passportStub.login(this.moderatorUser.username);
const { body: error } = await request(app)
.post('/api/user/generate')
Expand All @@ -399,14 +399,14 @@ describe('User API', () => {
.expect('Content-Type', /json/);
error.name.should.equal('InvalidPayloadError');
error.status.should.equal(400);
error.message.should.equal('Missing property legoUser from payload.');
error.message.should.equal('Missing property identifier from payload.');
});

it('should get an error when generating user with no email', async function () {
passportStub.login(this.moderatorUser.username);
const { body: error } = await request(app)
.post('/api/user/generate')
.send({ legoUser: 'correct', password: 'wrong' })
.send({ identifier: 'correct', password: 'wrong' })
.expect(400)
.expect('Content-Type', /json/);
error.name.should.equal('InvalidPayloadError');
Expand All @@ -422,11 +422,13 @@ describe('User API', () => {
.expect(201)
.expect('Content-Type', /json/);
bodyOne.status.should.equal('generated');
bodyOne.user.should.equal(genUserData.legoUser);
bodyOne.user.should.equal(genUserData.identifier);

// Check that the register index and the user was created
const register = await Register.findOne({ legoUser: genUserData.legoUser });
register.legoUser.should.equal(genUserData.legoUser);
const register = await Register.findOne({
identifier: genUserData.identifier,
});
register.identifier.should.equal(genUserData.identifier);
register.email.should.equal(genUserData.email);
const user = await User.findOne({ _id: register.user });
should.exist(user);
Expand All @@ -439,7 +441,7 @@ describe('User API', () => {
.expect(201)
.expect('Content-Type', /json/);
bodyTwo.status.should.equal('regenerated');
bodyTwo.user.should.equal(genUserData.legoUser);
bodyTwo.user.should.equal(genUserData.identifier);
});

it('should not be possible to generate the same user twice if they are active', async function () {
Expand All @@ -450,10 +452,12 @@ describe('User API', () => {
.expect(201)
.expect('Content-Type', /json/);
bodyOne.status.should.equal('generated');
bodyOne.user.should.equal(genUserData.legoUser);
bodyOne.user.should.equal(genUserData.identifier);

// Get the register and fake that they have logged in
const register = await Register.findOne({ legoUser: genUserData.legoUser });
const register = await Register.findOne({
identifier: genUserData.identifier,
});
register.user = null;
await register.save();

Expand Down

0 comments on commit 2c4ebd4

Please sign in to comment.