aboutsummaryrefslogtreecommitdiffstats
path: root/test/caldav/oauth2_test.js
blob: 9c3024c9278ad34eaa53f7d0b8f84645dc69917b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
testSupport.lib('querystring');
testSupport.lib('xhr');
testSupport.lib('oauth2');
testSupport.helper('fake_xhr');

suite('oauth', function() {
  var XHR;
  var FakeXhr;
  var OAuth;
  var QueryString;

  suiteSetup(function() {
    FakeXhr = Caldav.require('support/fake_xhr');
    XHR = Caldav.require('xhr');
    QueryString = Caldav.require('querystring');
    OAuth = Caldav.require('oauth2');
  });

  function mockTime() {
    setup(function() {
      this.clock = this.sinon.useFakeTimers();
    });

    teardown(function() {
      this.clock.restore();
    });
  }

  var subject;
  var apiCredentials = {
    url: 'http://foobar.com/',
    client_id: 'client_id',
    client_secret: 'client_secret',
    redirect_uri: 'redirect_uri'
  };

  setup(function() {
    subject = new OAuth(apiCredentials);
  });

  test('initialization', function() {
    assert.deepEqual(subject.apiCredentials, apiCredentials);
  });

  test('invalid credentials', function() {
    assert.throws(function() {
      new OAuth({ foo: 'bar' });
    }, /apiCredentials/);
  });

  suite('#authoriztionCode', function() {
    testSupport.mock.useFakeXHR();

    var code = 'codexxx';

    test('without code', function(done) {
      subject.authenticateCode(null, function(err) {
        assert.instanceOf(err, Error, 'sends an error');
        done();
      });
    });

    suite('success', function() {
      mockTime();

      var response = {
        access_token: 'token',
        refresh_token: 'refresh',
        expires_in: 3600,
        token_type: 'Bearer'
      };

      var expectedResponse;
      var expectedRequest;

      setup(function() {
        // copy expected properties over
        expectedResponse = {
          issued_at: Date.now()
        };

        for (var key in response) {
          expectedResponse[key] = response[key];
        }

        // expected post data
        expectedRequest = QueryString.stringify({
          code: code,
          client_id: subject.apiCredentials.client_id,
          client_secret: subject.apiCredentials.client_secret,
          redirect_uri: subject.apiCredentials.redirect_uri,
          grant_type: 'authorization_code'
        });
      });

      test('sending request', function(done) {
        var isComplete = false;
        var xhr = subject.authenticateCode(code, function(err, result) {
          assert.isNull(err);

          assert.isTrue(isComplete, 'completed assertions');
          assert.deepEqual(result, expectedResponse);
          done();
        });


        // verify xhr does the right thing
        assert.equal(xhr.sendArgs[0], expectedRequest, 'sends correct params');
        assert.equal(xhr.openArgs[0], 'POST', 'is HTTP post verb');
        assert.equal(xhr.openArgs[1], apiCredentials.url, 'opened with url');
        isComplete = true;

        xhr.respond(
          JSON.stringify(response),
          200,
          { 'Content-Type': 'application/json' }
        );
      });

      test('with username_info', function(done) {
        var userInfoData = { email: 'myfooba.com' };
        var userInfo = subject.apiCredentials.user_info = {
          url: 'http://google.com/',
          field: 'email'
        };

        var isComplete = false;
        var xhr = subject.authenticateCode(code, function(err, data) {
          assert.isNull(err, 'no error');
          assert.ok(isComplete, 'completed testing');
          expectedResponse.user = userInfoData.email;
          assert.deepEqual(data, expectedResponse);
          done();
        });

        xhr.respond(
          JSON.stringify(response),
          200,
          { 'Content-Type': 'application/json' }
        );

        var userInfoXhr =
          FakeXhr.instances[FakeXhr.instances.length - 1];

        assert.notEqual(userInfoXhr, xhr, 'issued userinfo');

        assert.equal(
          userInfoXhr.openArgs[1],
          userInfo.url
        );

        assert.equal(
          userInfoXhr.headers['Authorization'],
          response.token_type + ' ' + response.access_token,
          'sets access token'
        );

        isComplete = true;

        userInfoXhr.respond(
          JSON.stringify(userInfoData),
          200,
          { 'Content-Type': 'application/json' }
        );
      });

    });
  });

  suite('refreshToken', function() {
    test('without .refresh_token', function() {
      assert.throws(function() {
        subject.refreshToken(null, function() {});
      }, /token/);
    });

    suite('success', function() {
      testSupport.mock.useFakeXHR();
      mockTime();

      var refreshToken = 'mytokenfoo';
      var response = {
        access_token: 'newcode',
        expires_in: 3600,
        token_type: 'Bearer'
      };

      var expectedResponse;
      var expectedRequest;
      setup(function() {
        expectedResponse = {
          access_token: response.access_token,
          expires_in: response.expires_in,
          token_type: response.token_type,
          issued_at: Date.now()
        };

        expectedRequest = QueryString.stringify({
          refresh_token: refreshToken,
          client_id: subject.apiCredentials.client_id,
          client_secret: subject.apiCredentials.client_secret,
          grant_type: 'refresh_token'
        });
      });


      test('send request', function(done) {
        var isComplete = false;
        var xhr = subject.refreshToken(refreshToken, function(err, result) {
          assert.isNull(err);
          assert.isTrue(isComplete, 'assertions complete');
          assert.deepEqual(result, expectedResponse);
          done();
        });

        assert.deepEqual(xhr.sendArgs[0], expectedRequest, 'sent formdata');
        assert.equal(xhr.openArgs[1], apiCredentials.url, 'opened with url');
        isComplete = true;

        xhr.respond(
          JSON.stringify(response),
          200,
          { 'Content-Type': 'application/json' }
        );
      });
    });
  });

  suite('#accessTokenValid', function() {
    test('no access_token', function() {
      assert.isFalse(subject.accessTokenValid({ code: 'xxx' }));
    });

    test('access_token present but time invalid', function() {
      var oauth = {
        access_token: 'xxx',
        expires_in: 3600,
        issued_at: Date.now() - 3700
      };

      assert.isFalse(subject.accessTokenValid(oauth));
    });


    test('access_token present and not expired', function() {
      var oauth = {
        access_token: 'xxx',
        expires_in: 3600,
        issued_at: Date.now()
      };

      assert.isTrue(subject.accessTokenValid(oauth));
    });
  });


});