Completed
Push — master ( 416db6...de75b8 )
by Neil
01:28
created

auth.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 36
rs 8.8571
nop 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A auth.js ➔ ... ➔ beforeEach 0 20 1
A auth.js ➔ ... ➔ ??? 0 9 1
A auth.js ➔ ... ➔ afterEach 0 3 1
1
var chai = require('chai')
2
var server = require('../app')
3
var expect = chai.expect
4
var nock = require('nock')
5
/* eslint-disable no-unused-vars */
6
var should = chai.should()
7
8
describe('Auth', function () {
9
  it('login page loads correctly', function (done) {
10
    chai.request(server)
11
            .get('/auth/login')
12
            .end(function (err, res) {
13
              should.not.exist(err)
14
              res.should.have.status(200)
15
              done()
16
            })
17
  })
18
19
  it('google endpoint redirects', function (done) {
20
    chai.request(server)
21
        .get('/auth/google')
22
        .end(function (err, res) {
23
          should.not.exist(err)
24
          expect(res.redirects[0]).to.contain('https://accounts.google.com/o/oauth2/auth')
25
          done()
26
        })
27
  })
28
29
  it('it should end the session and show login', function (done) {
30
    chai.request(server)
31
            .get('/auth/logout')
32
            .end(function (err, res) {
33
              should.not.exist(err)
34
              expect(res.redirects[0]).to.match(/\/$/)
35
                // If successful layout displays Login links
36
              // TODO expect(res.text).to.match(/Contact/)
37
              done()
38
            })
39
  })
40
})
41
42
describe('GET /auth/google/callback', () => {
43
  beforeEach(function () {
44
    nock('https://accounts.google.com')
45
        .post('/o/oauth2/token')
46
        .reply(200, {
47
          access_token: 'test123828',
48
          token_type: 'Bearer',
49
          expires_in: 3600,
50
          refresh_token: 'test22912'
51
        })
52
    nock('https://www.googleapis.com')
53
        .get('/plus/v1/people/me')
54
        .query(true)
55
        .reply(200, {
56
          id: '1234567890',
57
          displayName: 'Test User'
58
        })
59
60
    var passport = require('passport')
61
    server.use(passport.initialize())
62
  })
63
64
  afterEach(function () {
65
    nock.cleanAll()
66
  })
67
68
  it('Login works', (done) => {
69
    chai.request(server)
70
        .get('/auth/google/callback?code=xxxxxxxx&authuser=0&session_state=xxxxxxxx&prompt=consent')
71
        .end(function (err, res) {
72
          should.not.exist(err)
73
          expect(res.redirects[0]).to.match(/\/$/) // redirects to home on success
74
          done()
75
        })
76
  })
77
})
78