|
1 |
| -import { expect, fetchMock } from '../test-helper'; |
2 |
| -import { ApplicationRecord, Author } from '../fixtures'; |
| 1 | +import { sinon, expect, fetchMock } from '../test-helper'; |
| 2 | +import { Config } from '../../src/index'; |
| 3 | +import { configSetup, ApplicationRecord, Author } from '../fixtures'; |
3 | 4 |
|
4 | 5 | after(function () {
|
5 | 6 | fetchMock.restore();
|
@@ -89,5 +90,110 @@ describe('authorization headers', function() {
|
89 | 90 | Author.all();
|
90 | 91 | });
|
91 | 92 | });
|
| 93 | + |
| 94 | + describe('local storage', function() { |
| 95 | + beforeEach(function() { |
| 96 | + Config.localStorage = { setItem: sinon.spy() } |
| 97 | + Config.jwtLocalStorage = 'jwt'; |
| 98 | + }); |
| 99 | + |
| 100 | + afterEach(function() { |
| 101 | + Config.localStorage = undefined; |
| 102 | + Config.jwtLocalStorage = undefined; |
| 103 | + }); |
| 104 | + |
| 105 | + describe('when configured to store jwt', function() { |
| 106 | + beforeEach(function() { |
| 107 | + Config.jwtLocalStorage = 'jwt'; |
| 108 | + }); |
| 109 | + |
| 110 | + it('updates localStorage on server response', function(done) { |
| 111 | + Author.all().then((response) => { |
| 112 | + let called = Config.localStorage.setItem |
| 113 | + .calledWith('jwt', 'somet0k3n'); |
| 114 | + expect(called).to.eq(true); |
| 115 | + done(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('uses the new jwt in subsequent requests', function(done) { |
| 120 | + Author.all().then((response) => { |
| 121 | + fetchMock.restore(); |
| 122 | + |
| 123 | + fetchMock.mock((url, opts) => { |
| 124 | + expect(opts.headers.Authorization).to.eq('Token token="somet0k3n"') |
| 125 | + done(); |
| 126 | + return true; |
| 127 | + }, 200); |
| 128 | + expect(Author.getJWT()).to.eq('somet0k3n'); |
| 129 | + expect(ApplicationRecord.jwt).to.eq('somet0k3n'); |
| 130 | + Author.all(); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('when JWT is already in localStorage', function() { |
| 135 | + beforeEach(function() { |
| 136 | + fetchMock.restore(); |
| 137 | + Config.localStorage['getItem'] = sinon.stub().returns('myt0k3n'); |
| 138 | + configSetup({ jwtLocalStorage: 'jwt' }); |
| 139 | + }); |
| 140 | + |
| 141 | + afterEach(function() { |
| 142 | + configSetup(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('sends it in initial request', function(done) { |
| 146 | + fetchMock.mock((url, opts) => { |
| 147 | + expect(opts.headers.Authorization).to.eq('Token token="myt0k3n"') |
| 148 | + done(); |
| 149 | + return true; |
| 150 | + }, 200); |
| 151 | + Author.find(1); |
| 152 | + }); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('when configured to NOT store jwt', function() { |
| 157 | + beforeEach(function() { |
| 158 | + Config.jwtLocalStorage = false; |
| 159 | + }); |
| 160 | + |
| 161 | + it('is does NOT update localStorage on server response', function(done) { |
| 162 | + Author.all().then((response) => { |
| 163 | + let called = Config.localStorage.setItem.called; |
| 164 | + expect(called).to.eq(false); |
| 165 | + done(); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('a write request', function() { |
| 173 | + beforeEach(function() { |
| 174 | + fetchMock.mock({ |
| 175 | + matcher: '*', |
| 176 | + response: { |
| 177 | + status: 200, |
| 178 | + body: { data: [] }, |
| 179 | + headers: { |
| 180 | + 'X-JWT': 'somet0k3n' |
| 181 | + } |
| 182 | + } |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + afterEach(function() { |
| 187 | + fetchMock.restore(); |
| 188 | + ApplicationRecord.jwt = null; |
| 189 | + }); |
| 190 | + |
| 191 | + it('also refreshes the jwt', function(done) { |
| 192 | + let author = new Author({ firstName: 'foo' }); |
| 193 | + author.save().then(() => { |
| 194 | + expect(ApplicationRecord.jwt).to.eq('somet0k3n'); |
| 195 | + done(); |
| 196 | + }); |
| 197 | + }); |
92 | 198 | });
|
93 | 199 | });
|
0 commit comments