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

feat(users): add first_pokemon_db and second_pokemon_db #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports.up = function (Knex, Promise) {
return Knex.schema.table('users', (table) => {
table.string('first_pokemon_db', 20).defaultTo('Bulbapedia');
table.string('second_pokemon_db', 20).defaultTo('Serebii');
});
};

exports.down = function (Knex, Promise) {
return Knex.schema.table('users', (table) => {
table.dropColumn('first_pokemon_db');
table.dropColumn('second_pokemon_db');
});
};
4 changes: 4 additions & 0 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = Bookshelf.model('User', Bookshelf.Model.extend({
username: this.get('username'),
friend_code_3ds: this.get('friend_code_3ds'),
friend_code_switch: this.get('friend_code_switch'),
first_pokemon_db: this.get('first_pokemon_db'),
second_pokemon_db: this.get('second_pokemon_db'),
date_created: this.get('date_created'),
date_modified: this.get('date_modified')
};
Expand All @@ -30,6 +32,8 @@ module.exports = Bookshelf.model('User', Bookshelf.Model.extend({
username: this.get('username'),
friend_code_3ds: this.get('friend_code_3ds'),
friend_code_switch: this.get('friend_code_switch'),
first_pokemon_db: this.get('first_pokemon_db'),
second_pokemon_db: this.get('second_pokemon_db'),
dexes,
donated: Boolean(this.get('stripe_id')),
date_created: this.get('date_created'),
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/features/users/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ exports.create = function (payload, request) {
password: payload.password,
friend_code_3ds: payload.friend_code_3ds,
friend_code_switch: payload.friend_code_switch,
first_pokemon_db: payload.first_pokemon_db,
second_pokemon_db: payload.second_pokemon_db,
referrer: payload.referrer,
last_ip: ip
}, { transacting })
Expand Down
2 changes: 2 additions & 0 deletions src/validators/users/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = Joi.object().keys({
string: { regex: { base: 'must be a valid Switch friend code' } }
}
}),
first_pokemon_db: Joi.string().max(20).empty(['', null]),
second_pokemon_db: Joi.string().max(20).empty(['', null]),
referrer: Joi.string().empty(['', null]),
title: Joi.string().max(300).trim().required(),
shiny: Joi.boolean().required(),
Expand Down
4 changes: 3 additions & 1 deletion src/validators/users/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ module.exports = Joi.object().keys({
language: {
string: { regex: { base: 'must be a valid Switch friend code' } }
}
})
}),
first_pokemon_db: Joi.string().max(20).empty(['', null]),
second_pokemon_db: Joi.string().max(20).empty(['', null])
});
4 changes: 4 additions & 0 deletions test/models/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('user model', () => {
'username',
'friend_code_3ds',
'friend_code_switch',
'first_pokemon_db',
'second_pokemon_db',
'date_created',
'date_modified'
]);
Expand All @@ -41,6 +43,8 @@ describe('user model', () => {
'username',
'friend_code_3ds',
'friend_code_switch',
'first_pokemon_db',
'second_pokemon_db',
'dexes',
'donated',
'date_created',
Expand Down
4 changes: 3 additions & 1 deletion test/plugins/features/users/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ describe('users controller', () => {

it('updates a user', () => {
const friendCode = '4321-4321-4321';
const firstPokemonDB = 'Bulbapedia';

return Controller.update(firstUser.username, { friend_code_3ds: friendCode }, { id: firstUser.id })
return Controller.update(firstUser.username, { friend_code_3ds: friendCode }, { id: firstUser.id }, { first_pokemon_db: firstPokemonDB })
.then(() => new User({ id: firstUser.id }).fetch())
.then((user) => {
expect(user.get('friend_code_3ds')).to.eql(friendCode);
expect(user.get('first_pokemon_db')).to.eql(firstPokemonDB);
});
});

Expand Down
82 changes: 82 additions & 0 deletions test/validators/users/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,88 @@ describe('users create validator', () => {

});

describe('first_pokemon_db', () => {

it('is optional', () => {
const data = { username: 'testing', password: 'testtest', title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.error).to.not.exist;
});

it('can be a string', () => {
const data = { username: 'testing', password: 'testtest', first_pokemon_db: 'Serebii', title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.error).to.not.exist;
expect(result.value.first_pokemon_db).to.eql(data.first_pokemon_db);
});

it('limits to 20 characters', () => {
const data = { username: 'testing', password: 'testtest', first_pokemon_db: 'a'.repeat(21), title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.error.details[0].path).to.eql('first_pokemon_db');
expect(result.error.details[0].type).to.eql('string.max');
});

it('converts null to undefined', () => {
const data = { username: 'testing', password: 'testtest', first_pokemon_db: null, title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.value.first_pokemon_db).to.be.undefined;
});

it('converts the empty string to undefined', () => {
const data = { username: 'testing', password: 'testtest', first_pokemon_db: '', title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.value.first_pokemon_db).to.be.undefined;
});

});

describe('second_pokemon_db', () => {

it('is optional', () => {
const data = { username: 'testing', password: 'testtest', title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.error).to.not.exist;
});

it('can be a string', () => {
const data = { username: 'testing', password: 'testtest', second_pokemon_db: 'Serebii', title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.error).to.not.exist;
expect(result.value.second_pokemon_db).to.eql(data.second_pokemon_db);
});

it('limits to 20 characters', () => {
const data = { username: 'testing', password: 'testtest', second_pokemon_db: 'a'.repeat(21), title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.error.details[0].path).to.eql('second_pokemon_db');
expect(result.error.details[0].type).to.eql('string.max');
});

it('converts null to undefined', () => {
const data = { username: 'testing', password: 'testtest', second_pokemon_db: null, title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.value.second_pokemon_db).to.be.undefined;
});

it('converts the empty string to undefined', () => {
const data = { username: 'testing', password: 'testtest', second_pokemon_db: '', title: 'Test', shiny: false, game: 'a', regional: true };
const result = Joi.validate(data, UsersCreateValidator);

expect(result.value.second_pokemon_db).to.be.undefined;
});

});

describe('referrer', () => {

it('is optional', () => {
Expand Down
66 changes: 66 additions & 0 deletions test/validators/users/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,70 @@ describe('users update validator', () => {

});

describe('first_pokemon_db', () => {

it('defaults to null', () => {
const data = {};
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.value.first_pokemon_db).to.be.undefined;
});

it('converts null to undefined', () => {
const data = { first_pokemon_db: null };
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.value.first_pokemon_db).to.be.undefined;
});

it('converts the empty string to undefined', () => {
const data = { first_pokemon_db: '' };
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.value.first_pokemon_db).to.be.undefined;
});

it('limits to 20 characters', () => {
const data = { first_pokemon_db: 'a'.repeat(21) };
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.error.details[0].path).to.eql('first_pokemon_db');
expect(result.error.details[0].type).to.eql('string.max');
});

});

describe('second_pokemon_db', () => {

it('defaults to undefined', () => {
const data = {};
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.value.second_pokemon_db).to.be.undefined;
});

it('converts null to undefined', () => {
const data = { second_pokemon_db: null };
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.value.second_pokemon_db).to.be.undefined;
});

it('converts the empty string to undefined', () => {
const data = { second_pokemon_db: '' };
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.value.second_pokemon_db).to.be.undefined;
});

it('limits to 20 characters', () => {
const data = { second_pokemon_db: 'a'.repeat(21) };
const result = Joi.validate(data, UsersUpdateValidator);

expect(result.error.details[0].path).to.eql('second_pokemon_db');
expect(result.error.details[0].type).to.eql('string.max');
});

});

});