describe("Test rule: objectID", () => { it("should validate ObjectID", () => { const rule: RuleObjectID = { type: "objectID", ObjectID }; const check = v.compile({ id: { type: "objectID", ObjectID } }); const message = "The 'id' field must be an valid ObjectID"; expect(check({ id: "5f082780b00cc7401fb8"})).toEqual([{ type: "objectID", field: "id", actual: "5f082780b00cc7401fb8", message }]); expect(check({ id: new ObjectID() })).toEqual(true); const o = { id: "5f082780b00cc7401fb8e8fc" }; expect(check(o)).toEqual(true); expect(o.id).toBe("5f082780b00cc7401fb8e8fc"); }); it("should convert hexString-objectID to ObjectID", () => { const check = v.compile({ id: { type: "objectID", ObjectID, convert: true } }); const oid = new ObjectID(); const o = { id: oid.toHexString() }; expect(check(o)).toEqual(true); expect(o.id).toBeInstanceOf(ObjectID); expect(o.id).toEqual(oid); }); });
const check = v.compile({ id: { type: "objectID", ObjectID, convert: true } }); const oid = new ObjectID(); const o = { id: oid.toHexString() };
describe('user.generateAuthToken', () => { test('Should return a valid JWT', () => { const payload = { _id: new mongoose.Types.ObjectId().toHexString() }; const user = new User(payload); const token = user.generateAuthToken(); const decoded = jwt.verify(token, config.get('jwtPrivateKey')); expect(decoded).toMatchObject(payload); }); });
_generateId () { return new ObjectID().toHexString() }
const processDocumentEntry = function (zipEntry, doc, user, options) { // If the document is already init, import this one and prepare a new one... let action = Promise.resolve() if (doc.id) { action = importDocument(Object.assign({}, doc), options) } doc = { id: options.keepOriginalId ? zipEntry.entryName.slice(0, -1) : new ObjectID().toHexString(), owner: user.id, attachments: [], labels: [] } return action.then(function () { return Promise.resolve(doc) }) }
describe('GET /api/tutorials/:tutorial', () => { test('should return tutorial with given id', done => { request(app) .get(`/api/tutorials/${tutorials[0]._id.toHexString()}`) .expect(200) .expect(res => { expect(res.body.tutorial._id).toBe(tutorials[0]._id.toHexString()); }) .end(done); }); test('should return 404 if tutorial not found with given id', done => { const id = new mongoose.Types.ObjectId(); request(app) .get(`/api/tutorals/${id}`) .expect(404) .end(done); }); });
.expect(res => { expect(res.body.text).toBe(text); expect(res.body._creator).toBe(usersDummy[0]._id.toHexString()); }) .end((err, res) => { Todo.findById(res.body._id).then(todo => { expect(todo.text).toBe(text); expect(todo._creator.toHexString()).toBe(usersDummy[0]._id.toHexString()); }).then( Todo.find().then(todos => {
describe('GET /search/savedpins/:id', () => { it('should return savedpins doc', (done) => { request(app) .get(`/search/savedpins/${pins[0]._id.toHexString()}`) .set('x-access-token', users[0].tokens[0].token) .expect(200) const hexId = new ObjectID().toHexString();
describe('user.genAuthToken', () => { it('should generate a valid JSON Web Token', () => { const _id = mongoose.Types.ObjectId().toHexString(); // OBS: new User({...}) doesn't save the user into the DB, only in memory const user = new User({ _id, email: 'email@example.com' }); // Don't call user.save() to avoid storing data into DB const token = user.genAuthToken(); const decoded = jwt.verify(token, JWT_PRIVATE_KEY); expect(decoded).toMatchObject({ _id }); }); });
.post(`/api/tutorials/review/${tutorials[0]._id.toHexString()}`) .set('Authorization', userOneToken) .send(review) .post(`/api/tutorials/review/${id.toHexString()}`) .set('Authorization', userOneToken) .send(review) .post(`/api/tutorials/review/${tutorials[0]._id.toHexString()}`) .send(review) .expect(401)
describe('GET /todos',()=>{ it('should get all todos',(done)=>{ request(app) .get('/todos') .expect(200) .expect((res)=>{ expect(res.body.todos.length).toBe(3); }) .end(done); }); it('should get todos by id ',(done)=>{ var id="5c586bcb0b9cf311fafb86b2"; request(app) .get(`/todos/${id}`) .expect(200) .expect((res)=>{ expect(res.body.todo._id).toBe('5c586bcb0b9cf311fafb86b2'); }) .end(done); }); it('should 404 on invalid todos by id ',(done)=>{ var id=new ObjectID().toHexString(); request(app) .get(`/todos/${id}`) .expect(404) .end(done); }); });
describe('DELETE /search/savedpins/:id', () => { it('should remove a single searchpins', (done) => { const hexId = pins[0]._id.toHexString(); const hexId = new ObjectID().toHexString();
.expect(200) .expect(res => { expect(res.body._id).toBe(usersDummy[0]._id.toHexString()); expect(res.body.email).toBe(usersDummy[0].email); }).end(done);
describe('auth middleware', () => { test('should populate req.user with the payload of the valid JWT', () => { const user = { _id: mongoose.Types.ObjectId().toHexString() } const token = new User(user).generateAuthToken(); const req = { cookies: { token } } const res = {} const next = jest.fn(); auth(req, res, next) expect(req.user).toMatchObject(user); }) })
describe("GET /todos/:id", () => { it("should get a todo by its id", done => { request(app) .get(`/todos/${todosDumi[0]._id.toHexString()}`) .expect(200) .expect(res => { var hexId = new ObjectID().toHexString();