Skip to content

clement9527/users

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TOC

Authenticate

POST /auth

POST /auth with unregistered email and password will register the email, returning status 200 and authentication token.

request(url)
        .post('/auth')
        .send({email: 'test@email.com', password: 'password'})
        .end(function(err, res) {
                 res.status.should.eql(200);
                 res.body.should.have.property('token');
                 Admin.findOne({email: 'test@email.com'}, function(err, result){
                     result.password.should.eql('password');
                     done();
                 });
             });

POST /auth with registered email but invalid password will return status 403.

request(url)
        .post('/auth')
        .send({email: existingAdmin.email, password: 'invalid_password'})
        .end(function(err, res) {
                 res.status.should.eql(403);
                 done();
             });

POST /auth with registered email and valid password will return status 200 and authentication token.

request(url)
        .post('/auth')
        .send({email: existingAdmin.email, password: existingAdmin.password})
        .end(function(err, res) {
                 res.status.should.eql(200);
                 done();
             });

Manage Users

GET /users

GET /users will return all users.

//user1 and user2 are existing in database
request(url)
    .get('/users')
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(200);
        res.body.should.have.length(2);
        res.body[0].should.have.property('userName', user1.userName);
        res.body[1].should.have.property('userName', user2.userName);
        done();
    });

GET /users:id with valid id will return status 200 and the user.

//user1 and user2 are existing in database
request(url)
    .get('/users/' + user1.id)
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(200);
        res.body.should.have.property('userName', 'theUserName1');
        done();
    });

GET /users/:id with invalid id will return status 500.

//user1 and user2 are existing in database
request(url)
    .get('/users/invalid_id')
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(500);
        res.body.should.eql({});
        done();
    });

POST /users

POST /users with valid user will return status 200 and create that user.

var user = new User({userName: 'theUserName', givenName: 'theGivenName', surName: 'theSurName'});
request(url)
    .post('/users')
    .send(user)
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(200);
        res.body.should.have.property('userName', user.userName);
        res.body.should.have.property('givenName', user.givenName);
        res.body.should.have.property('surName', user.surName);
        done();
    });

POST /users with duplicated username will return status 500.

request(url)
    .post('/users')
    .send(new User({userName: 'existingUserName'}))
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(500);
        done();
    });

POST /users with required field missing will return status 500.

var user = {
    givenName: 'theGivenName',
    surName: 'theSurName'
};
request(url)
    .post('/users')
    .send(user)
    .set('authorization', token)
    .end(function(err, res){
        res.status.should.eql(500);
        done();
    });

DELETE /users/:id

DELETE /users/:id with valid id will return status 200 and delete that user.

request(url)
    .delete('/users/' + user.id)
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(200);
        User.findById(user.id, function (err, result) {
            should.not.exist(result);
            done();
        });
    });

DELETE /users/:id with invalid id will return status 500.

request(url)
    .delete('/users/invalid_id')
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(500);
        done();
    });

PUT /users/:id

PUT /users/:id with different username will return status 200 and update the user.

user.userName = 'anotherUserName';
request(url)
    .put('/users/' + user.id)
    .send(user)
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(200);
        User.findById(user.id, function (err, result) {
            result.should.have.property('userName', 'anotherUserName');
            done();
        });
    });

PUT /users/:id with invalid id will return status 500.

user.userName = 'anotherUserName';
request(url)
    .put('/users/invalid_id')
    .send(user)
    .set('authorization', token)
    .end(function (err, res) {
        res.status.should.eql(500);
        done();
    });

About

Experimenting restful service in node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published