Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This file contains code that is sourced from
  2. //https://pastebin.com/pg0kkdU9 , author:Mr Kushira
  3.  
  4. //------------------------------------
  5. //Basic config
  6. const SERVER_URL = 'http://localhost:3001';
  7.  
  8. //sample data
  9. const SAMPLE_USERNAME = 'test123';
  10. const SAMPLE_PASSWORD = 'test123';
  11. const SAMPLE_POSTID = '597ebeb24317393af33076a2';
  12. const CORRUPT_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QxMjMiLCJpYXQiOjE1MDEzOTQyOmV4cCI6MTUwMTU2NzA5M30.4mzC72AqPBY0JGf3_uCOymFQd72h0g2wPPlil3r6-38';
  13. //-----------------------
  14.  
  15. const should = require('should');
  16. const request = require('supertest');
  17. const agent = request.agent(SERVER_URL);
  18.  
  19. describe('testing api calls', () => {
  20.  
  21.     it('should login when valid details are given', done => {
  22.         agent.post('/users/login')
  23.             .send({
  24.                 'username': SAMPLE_USERNAME,
  25.                 'password': SAMPLE_PASSWORD
  26.             })
  27.             .expect(200)
  28.             .end((err, res) => {
  29.                 if (err) {
  30.                     return done(err);
  31.                 }
  32.                 res.body.should.be.an.Object().and.have.property('access_token');
  33.                 done();
  34.             });
  35.     });
  36.  
  37.     it('should reject invalid user tokens', done => {
  38.         agent.post('/isloggedin')
  39.             .set('authorization', 'JWT ' + CORRUPT_TOKEN)
  40.             .send('')
  41.             .expect(401)
  42.             .end((err, res) => {
  43.                 if (err) {
  44.                     return done(err);
  45.                 }
  46.                 done();
  47.             });
  48.     });
  49.  
  50.     it('should return all public posts by a uesr', done => {
  51.         agent.get('/users/' + SAMPLE_USERNAME + '/posts/')
  52.             .expect(200)
  53.             .end((err, res) => {
  54.                 if (err) {
  55.                     return done(err);
  56.                 }
  57.                 res.body.should.be.an.Object().and.have.property('posts');
  58.                 done();
  59.             });
  60.  
  61.     });
  62.  
  63.     it('should return a public post by a user', done => {
  64.         agent.get('/users/' + SAMPLE_USERNAME + '/posts/' + SAMPLE_POSTID)
  65.             .expect(200)
  66.             .end((err, res) => {
  67.                 if (err) {
  68.                     return done(err);
  69.                 }
  70.                 res.body.should.be.an.Object().and.have.property('posts');
  71.                 done();
  72.             });
  73.  
  74.     });
  75.  
  76.     it('should disallow an unathorized user to post activities', done => {
  77.         agent.post('/newactivity')
  78.             .expect(401)
  79.             .set('authorization', 'JWT ' + CORRUPT_TOKEN)
  80.             .end((err, res) => {
  81.                 if (err) {
  82.                     return done(err);
  83.                 }
  84.                 done();
  85.             });
  86.  
  87.     });
  88.  
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement