@academyjs/rover
Version:
Rover allows you to learn programming interactively.
218 lines • 17.3 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const User = require("../../app/model/user");
const { users, assertThrowsAsync } = require("../helper");
describe("User model", () => {
let userTemplate = null;
beforeEach(() => {
[userTemplate] = users;
});
afterEach(() => {
userTemplate = null;
});
it("should be created with correct data", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign({}, userTemplate));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
}));
it("should not be created when first name is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { firstName: undefined }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The first name attribute is required.");
}));
it("should not be created when first name is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { firstName: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The first name attribute is required.");
}));
it("should not be created when first name is empty", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { firstName: "" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The first name attribute is required.");
}));
it("should be created when first name length is 1", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { firstName: "A" }));
yield user.save();
// TODO
}));
it("should not be created when first name is too long", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { firstName: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The first name attribute is should a maximum of 30 characters.");
}));
it("should not be created when last name is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { lastName: undefined }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The last name attribute is required.");
}));
it("should not be created when last name is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { lastName: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The last name attribute is required.");
}));
it("should not be created when last name is empty", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { lastName: "" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The last name attribute is required.");
}));
it("should be created when last name length is 1", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { lastName: "A" }));
yield user.save();
// TODO
}));
it("should not be created when last name is too long", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { lastName: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The last name attribute should not be longer than 30 characters.");
}));
it("should not be created when user name is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { userName: undefined }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The user name attribute is required.");
}));
it("should not be created when user name is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { userName: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The user name attribute is required.");
}));
it("should not be created when user name is empty", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { userName: "" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The user name attribute is required.");
}));
it("should not be created when user name is too short", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { userName: "a" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The user name attribute should be at least 3 characters long.");
}));
it("should not be created when user name is too long", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { userName: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The user name attribute should not be longer than 30 characters.");
}));
// it("should not be created when user name does not match regex", async () => {
// const user = new User({
// ...userTemplate,
// userName: "$$$@@@@",
// });
// await assertThrowsAsync(
// async () => user.save(),
// "The user name attribute should match the regex pattern."
// );
// });
it("should not be created when user name is not unique", () => __awaiter(this, void 0, void 0, function* () {
/* Load the indexes created by MongoDB. */
yield User.init();
const user1 = new User(Object.assign(Object.assign({}, userTemplate), { userName: "itssamuelrowe" }));
yield user1.save();
assert.isFalse(user1.isNew, "The first user should be persisted to the database.");
const user2 = new User(Object.assign(Object.assign({}, users[1]), { userName: "itssamuelrowe" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user2.save(); }), "The user name attribute should be unique.");
}));
it("when about is undefined, should be created with null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { about: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.about, null, "The about attribute should default to null.");
}));
it("should be created when the about attribute is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { about: null }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.about, null, "The about attribute should accept null values.");
}));
it("should be created when about is empty", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { about: "" }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.about, "", "The about attribute should be an empty string.");
}));
it("should not be created when about is too long", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { about: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAA" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The about attribute should not be longer than 512 characters.");
}));
it("should be created when gender is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { gender: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.gender, undefined, "The gender attribute should default to undefined.");
}));
it("should not be created when gender is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { gender: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The gender attribute should not be null.");
}));
it("should not be created when gender is invalid", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { gender: "invalid_gender_value" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The gender attribute should be valid.");
}));
it("should be created when country code is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { countryCode: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.countryCode, undefined, "The country code attribute should default to undefined.");
}));
it("should not be created when country code is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { countryCode: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The country code attribute should not be null.");
}));
it("should not be created when country code is invalid", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { countryCode: "invalid_country_code_value" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The country code attribute should be valid.");
}));
it("should not created when email address is not unique", () => __awaiter(this, void 0, void 0, function* () {
/* Load the indexes created by MongoDB. */
yield User.init();
const user1 = new User(Object.assign(Object.assign({}, userTemplate), { emailAddress: "samuel@blogpod.io" }));
yield user1.save();
assert.isFalse(user1.isNew, "The first user should be persisted to the database.");
const user2 = new User(Object.assign(Object.assign({}, users[1]), { emailAddress: "samuel@blogpod.io" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user2.save(); }), "The email address attribute should be unique.");
}));
it("when email verified attribute is missing, should be created with default as false", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { emailVerified: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.isFalse(user.emailVerified, "The email verified attribute should false.");
}));
it("should be created with null, when picture URL attribute is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { pictureURL: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.pictureURL, null, "The picture URL attribute should null.");
}));
it("should be created with picturl URL as null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { pictureURL: null }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.pictureURL, null, "The picture URL attribute should null.");
}));
it("should be created with 'en', when displayLanguageCode attribute is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { displayLanguageCode: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.displayLanguageCode, "en", "The displayLanguageCode attribute should 'en'.");
}));
it("should not be created when displayLanguageCode is invalid", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { displayLanguageCode: "invalid_display_language_code_value" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The displayLanguageCode attribute should not be invalid.");
}));
it("should not created when displayLanguageCode attribute is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { displayLanguageCode: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The displayLanguageCode attribute should not be null.");
}));
it("should be created with 'active', when status attribute is undefined", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { status: undefined }));
yield user.save();
assert.isFalse(user.isNew, "The user should be persisted to the database.");
assert.strictEqual(user.status, "active", "The status attribute should 'active'.");
}));
it("should not be created when status is invalid", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { status: "invalid_status_value" }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The status attribute should not be invalid.");
}));
it("should not created when status attribute is null", () => __awaiter(this, void 0, void 0, function* () {
const user = new User(Object.assign(Object.assign({}, userTemplate), { status: null }));
yield assertThrowsAsync(() => __awaiter(this, void 0, void 0, function* () { return user.save(); }), "The status attribute should not be null.");
}));
// TODO: roles, birthday, interests, contentLanguageCodes
});
//# sourceMappingURL=user.test.js.map