1 | Helper = require('hubot-test-helper')
|
2 | chai = require 'chai'
|
3 | nock = require 'nock'
|
4 |
|
5 | expect = chai.expect
|
6 |
|
7 | helper = new Helper('../src/fitbit-leaders.coffee')
|
8 |
|
9 | describe 'hubot-fitbit-leaders', ->
|
10 | beforeEach ->
|
11 | process.env.HUBOT_LOG_LEVEL = 'error'
|
12 | nock.disableNetConnect()
|
13 |
|
14 | afterEach ->
|
15 | nock.cleanAll()
|
16 | delete process.env.HUBOT_LOG_LEVEL
|
17 |
|
18 | context 'basic tests', ->
|
19 | beforeEach ->
|
20 | process.env.HUBOT_LOG_LEVEL = 'error'
|
21 | process.env.FITBIT_CLIENT_ID = 'abc123'
|
22 | process.env.FITBIT_CLIENT_SECRET = '123abc456efg'
|
23 | process.env.FITBIT_OAUTH_TOKEN = 'hijk123abc456efg789lmnop'
|
24 | @room = helper.createRoom()
|
25 |
|
26 | afterEach ->
|
27 | @room.destroy()
|
28 | delete process.env.HUBOT_LOG_LEVEL
|
29 | delete process.env.FITBIT_CLIENT_ID
|
30 | delete process.env.FITBIT_CLIENT_SECRET
|
31 | delete process.env.FITBIT_OAUTH_TOKEN
|
32 |
|
33 |
|
34 | it 'returns the leaderboard of your friends', (done) ->
|
35 | nock('https://api.fitbit.com:443')
|
36 | .get('/1.2/user/-/friends/leaderboard.json')
|
37 | .replyWithFile(200, __dirname + '/fixtures/leaderboard.json')
|
38 |
|
39 | selfRoom = @room
|
40 | selfRoom.user.say('alice', '@hubot fitbit leaders')
|
41 | setTimeout(() ->
|
42 | try
|
43 | expect(selfRoom.messages).to.eql [
|
44 | ['alice', '@hubot fitbit leaders']
|
45 | ['hubot', "\n#1 Nick - 56,000\n#2 Fitbit U. - 45,000"]
|
46 | ]
|
47 | done()
|
48 | catch err
|
49 | done err
|
50 | return
|
51 | , 1000)
|
52 |
|
53 |
|
54 | it 'returns setup instructions', (done) ->
|
55 | selfRoom = @room
|
56 | selfRoom.user.say('alice', '@hubot fitbit setup')
|
57 | setTimeout(() ->
|
58 | try
|
59 | expect(selfRoom.messages).to.eql [
|
60 | ['alice', '@hubot fitbit setup']
|
61 | ['hubot', "1) Go to: https://www.fitbit.com/oauth2/authorize?response_type=token&client_id=abc123&redirect_uri=<YOUR REDIRECT URL>&scope=profile%20social&expires_in=31536000\n2) Save the URL token in the bot's configuration\n3) Restart Hubot to load configuration"]
|
62 | ]
|
63 | done()
|
64 | catch err
|
65 | done err
|
66 | return
|
67 | , 1000)
|
68 |
|
69 |
|
70 | it 'returns registration instructions', (done) ->
|
71 | nock('https://api.fitbit.com:443')
|
72 | .get('/1.2/user/-/profile.json')
|
73 | .replyWithFile(200, __dirname + '/fixtures/profile.json')
|
74 |
|
75 | selfRoom = @room
|
76 | selfRoom.user.say('alice', '@hubot fitbit register')
|
77 | setTimeout(() ->
|
78 | try
|
79 | expect(selfRoom.messages).to.eql [
|
80 | ['alice', '@hubot fitbit register']
|
81 | ['hubot', '1) Add Fitbit as a friend - http://fitbit.com/user/257V3V\n2) Type `hubot fitbit approve`']
|
82 | ]
|
83 | done()
|
84 | catch err
|
85 | done err
|
86 | return
|
87 | , 1000)
|
88 |
|
89 |
|
90 | it 'returns a list of friends', (done) ->
|
91 | nock('https://api.fitbit.com:443')
|
92 | .get('/1.2/user/-/friends.json')
|
93 | .replyWithFile(200, __dirname + '/fixtures/friends.json')
|
94 |
|
95 | selfRoom = @room
|
96 | selfRoom.user.say('alice', '@hubot fitbit friends')
|
97 | setTimeout(() ->
|
98 | try
|
99 | expect(selfRoom.messages).to.eql [
|
100 | ['alice', '@hubot fitbit friends']
|
101 | ['hubot', 'Nick, Fitbit U.']
|
102 | ]
|
103 | done()
|
104 | catch err
|
105 | done err
|
106 | return
|
107 | , 1000)
|
108 |
|
109 |
|
110 | it 'approves pending friend requests', (done) ->
|
111 | nock('https://api.fitbit.com:443')
|
112 | .get('/1.2/user/-/friends/invitations.json')
|
113 | .replyWithFile(200, __dirname + '/fixtures/invitations.json')
|
114 | nock('https://api.fitbit.com:443')
|
115 | .post('/1.2/user/-/friends/invitations/257V3V.json')
|
116 | .replyWithFile(200, __dirname + '/fixtures/invitations-257V3V.json')
|
117 |
|
118 | selfRoom = @room
|
119 | selfRoom.user.say('alice', '@hubot fitbit approve')
|
120 | setTimeout(() ->
|
121 | try
|
122 | expect(selfRoom.messages).to.eql [
|
123 | ['alice', '@hubot fitbit approve']
|
124 | ['hubot', 'Approve: Nick']
|
125 | ]
|
126 | done()
|
127 | catch err
|
128 | done err
|
129 | return
|
130 | , 1000)
|
131 |
|
132 | context 'expired token', ->
|
133 | beforeEach ->
|
134 | process.env.HUBOT_LOG_LEVEL = 'error'
|
135 | process.env.FITBIT_CLIENT_ID = 'abc123'
|
136 | process.env.FITBIT_CLIENT_SECRET = '123abc456efg'
|
137 | process.env.FITBIT_OAUTH_TOKEN = 'hijk123abc456efg789lmnop'
|
138 | @room = helper.createRoom()
|
139 |
|
140 | afterEach ->
|
141 | @room.destroy()
|
142 | delete process.env.HUBOT_LOG_LEVEL
|
143 | delete process.env.FITBIT_CLIENT_ID
|
144 | delete process.env.FITBIT_CLIENT_SECRET
|
145 | delete process.env.FITBIT_OAUTH_TOKEN
|
146 |
|
147 |
|
148 | it 'display an error for an expired token', (done) ->
|
149 | nock('https://api.fitbit.com:443')
|
150 | .get('/1.2/user/-/friends/leaderboard.json')
|
151 | .replyWithFile(401, __dirname + '/fixtures/error-token-expired.json')
|
152 |
|
153 | selfRoom = @room
|
154 | selfRoom.user.say('alice', '@hubot fitbit leaders')
|
155 | setTimeout(() ->
|
156 | try
|
157 | expect(selfRoom.messages).to.eql [
|
158 | ['alice', '@hubot fitbit leaders']
|
159 | ['hubot', 'Your Fitbit token has expired! See `hubot fitbit token` to set up a new one.']
|
160 | ]
|
161 | done()
|
162 | catch err
|
163 | done err
|
164 | return
|
165 | , 1000)
|