1 | const { expect } = require('chai');
|
2 |
|
3 | const Credentials = require('../src/Credentials.js');
|
4 |
|
5 | describe('Credentials', () => {
|
6 | it('should require a consumer key and secret', () => {
|
7 | expect(() => new Credentials({})).to.throw(
|
8 | 'Missing required argument: consumer_key'
|
9 | );
|
10 |
|
11 | expect(() => new Credentials({ consumer_key: 'foo' })).to.throw(
|
12 | 'Missing required argument: consumer_secret'
|
13 | );
|
14 |
|
15 | expect(
|
16 | () => new Credentials({ consumer_key: 'foo', consumer_secret: 'bar' })
|
17 | ).to.not.throw();
|
18 | });
|
19 |
|
20 | it('should enforce access key and secret parity', () => {
|
21 | expect(
|
22 | () =>
|
23 | new Credentials({
|
24 | consumer_key: 'foo',
|
25 | consumer_secret: 'bar',
|
26 | access_token_key: 'qux',
|
27 | })
|
28 | ).to.throw(
|
29 | 'Invalid argument: access_token_key and access_token_secret must both ' +
|
30 | 'be defined when using user authorization'
|
31 | );
|
32 |
|
33 | expect(
|
34 | () =>
|
35 | new Credentials({
|
36 | consumer_key: 'foo',
|
37 | consumer_secret: 'bar',
|
38 | access_token_secret: 'qux',
|
39 | })
|
40 | ).to.throw(
|
41 | 'Invalid argument: access_token_key and access_token_secret must both ' +
|
42 | 'be defined when using user authorization'
|
43 | );
|
44 |
|
45 | expect(
|
46 | () =>
|
47 | new Credentials({
|
48 | consumer_key: 'foo',
|
49 | consumer_secret: 'bar',
|
50 | access_token_key: 'qux',
|
51 | access_token_secret: 'quz',
|
52 | })
|
53 | ).to.not.throw();
|
54 | });
|
55 |
|
56 | it('should disallow using bearer token with user auth', () => {
|
57 | expect(
|
58 | () =>
|
59 | new Credentials({
|
60 | consumer_key: 'foo',
|
61 | consumer_secret: 'bar',
|
62 | bearer_token: 'invalid',
|
63 | access_token_key: 'qux',
|
64 | access_token_secret: 'quz',
|
65 | })
|
66 | ).to.throw(
|
67 | 'Invalid argument: access_token_key and access_token_secret should not ' +
|
68 | 'be used with bearer_token'
|
69 | );
|
70 | });
|
71 |
|
72 | it('should recognize app vs user auth', () => {
|
73 | let credentials = new Credentials({
|
74 | consumer_key: 'foo',
|
75 | consumer_secret: 'bar',
|
76 | });
|
77 | expect(credentials.appAuth()).to.be.true;
|
78 | expect(credentials.userAuth()).to.be.false;
|
79 |
|
80 | credentials = new Credentials({
|
81 | consumer_key: 'foo',
|
82 | consumer_secret: 'bar',
|
83 | bearer_token: 'qux',
|
84 | });
|
85 | expect(credentials.appAuth()).to.be.true;
|
86 | expect(credentials.userAuth()).to.be.false;
|
87 |
|
88 | credentials = new Credentials({
|
89 | consumer_key: 'foo',
|
90 | consumer_secret: 'bar',
|
91 | access_token_key: 'qux',
|
92 | access_token_secret: 'quz',
|
93 | });
|
94 | expect(credentials.appAuth()).to.be.false;
|
95 | expect(credentials.userAuth()).to.be.true;
|
96 | });
|
97 | });
|