Completed
Push — master ( 2d8abe...e6d75a )
by Neil
03:16
created

auth.js ➔ ???   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

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

3 Functions

Rating   Name   Duplication   Size   Complexity  
A auth.js ➔ ... ➔ ??? 0 17 2
A auth.js ➔ ... ➔ afterEach 0 3 1
A auth.js ➔ ... ➔ beforeEach 0 19 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 logged out', 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('Account page logged out redirects', (done) => {
20
    chai.request(server)
21
      .get('/account').end(function (err, res) {
22
        should.not.exist(err)
23
        expect(res.redirects[0]).to.match(/\/auth\/login/)
24
        expect(res).to.redirect
0 ignored issues
show
introduced by
The result of the property access to expect(res).to.redirect is not used.
Loading history...
25
        done()
26
      })
27
  })
28
29
  it('google endpoint redirects', function (done) {
30
    chai.request(server)
31
      .get('/auth/google')
32
      .end(function (err, res) {
33
        if (err) console.log(err.stack)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
34
        expect(res.redirects[0]).to.contain('https://accounts.google.com/o/oauth2/auth')
35
        expect(res).to.redirect
0 ignored issues
show
introduced by
The result of the property access to expect(res).to.redirect is not used.
Loading history...
36
        done()
37
      })
38
  })
39
40
  it('it should end the session and show login', function (done) {
41
    chai.request(server)
42
      .get('/auth/logout')
43
      .end(function (err, res) {
44
        should.not.exist(err)
45
        expect(res.redirects[0]).to.match(/\/$/)
46
        expect(res).to.redirect
0 ignored issues
show
introduced by
The result of the property access to expect(res).to.redirect is not used.
Loading history...
47
        // If successful layout displays Login links
48
        // TODO expect(res.text).to.match(/Contact/)
49
        done()
50
      })
51
  })
52
})
53
54
describe('GET /auth/google/callback', () => {
55
  beforeEach(function () {
56
    nock('https://accounts.google.com')
57
      .post('/o/oauth2/token')
58
      .reply(200, {
59
        access_token: 'test123828',
60
        token_type: 'Bearer',
61
        expires_in: 3600,
62
        refresh_token: 'test22912'
63
      })
64
    nock('https://www.googleapis.com')
65
      .get('/plus/v1/people/me')
66
      .query(true)
67
      .reply(200, {
68
        id: '1234567890',
69
        displayName: 'Test User',
70
        emails: ['[email protected]'],
71
        username: 'dueyfinster'
72
      })
73
  })
74
75
  afterEach(function () {
76
    nock.cleanAll()
77
  })
78
79
  it('Login works and shows account', (done) => {
80
    var agent = chai.request.agent(server)
81
    try {
82
      agent
83
        .get('/auth/google/callback?code=xxxxxxxx&authuser=0&session_state=xxxxxxxx&prompt=consent')
84
        .then(function (res) {
0 ignored issues
show
Unused Code introduced by
The parameter res is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
85
          return agent.get('/account')
86
            .then(function (res) {
87
              res.should.have.status(200)
88
              res.text.should.include('Test User')
89
              done()
90
            })
91
        })
92
    } catch (error) {
93
      done(error)
94
    }
95
  })
96
})
97