Skip to content

Commit b7f2cd3

Browse files
committed
fix(header): Fixed unauthorization and unit tests
1 parent 7973a74 commit b7f2cd3

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

src/ws-header/authorization.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class Authorization {
9898
* @returns {void}
9999
*/
100100
unauthorize() {
101-
this.storage.remove('access_key');
101+
this.storage.remove('access_token');
102102
this.storage.remove('expires_at');
103103
this.changeAccessToken(null);
104104
}

src/ws-header/ws-header.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import {LocalStorage} from './storage/local-storage';
44
import {Authorization} from './authorization';
55
import {WSDropdown} from '../ws-dropdown/ws-dropdown';
66

7-
let authorization;
8-
97
/**
108
* This component renders a generic header which provides authentication and language management
119
*
@@ -15,6 +13,11 @@ let authorization;
1513
*/
1614
export class WSHeader extends Component {
1715

16+
/**
17+
* @type {Authorization}
18+
*/
19+
static authorization = undefined;
20+
1821
/**
1922
* Default storage instance
2023
* @type {AbstractStorage}
@@ -64,11 +67,20 @@ export class WSHeader extends Component {
6467
* @returns {string|null}
6568
*/
6669
static getAccessToken(queryString = location.hash.substr(1)) {
67-
authorization = authorization || new Authorization(this.storage);
68-
if (!authorization.accessToken) {
69-
authorization.tryFetchToken(queryString);
70+
this.authorization = this.authorization || new Authorization(this.storage);
71+
if (!this.authorization.accessToken) {
72+
this.authorization.tryFetchToken(queryString);
7073
}
71-
return authorization.accessToken;
74+
return this.authorization.accessToken;
75+
}
76+
77+
/**
78+
* Unauthorize will remove the access token from storage
79+
* @returns {void}
80+
*/
81+
static removeAccessToken() {
82+
this.authorization = this.authorization || new Authorization(this.storage);
83+
this.authorization.unauthorize();
7284
}
7385

7486
/**
@@ -141,9 +153,9 @@ export class WSHeader extends Component {
141153
*/
142154
initAuthorization(props) {
143155
// Initialize authorization with implicit flow
144-
authorization = authorization || new Authorization(WSHeader.storage);
156+
this.constructor.authorization = this.constructor.authorization || new Authorization(WSHeader.storage);
145157
// Listen to authorization changes
146-
authorization.onAccessTokenChange(accessToken => {
158+
this.constructor.authorization.onAccessTokenChange(accessToken => {
147159
if (this.mounted) {
148160
this.setState({isLoggedIn: !!accessToken});
149161
} else {
@@ -153,14 +165,14 @@ export class WSHeader extends Component {
153165
this.dispatchEvent('ws-auth-changed', accessToken);
154166
});
155167
// Check if we was redirected from the auth page and an access token is available
156-
authorization.tryFetchToken(location.hash.substr(1));
168+
this.constructor.authorization.tryFetchToken(location.hash.substr(1));
157169
// Listen for authentication requests
158170
window.addEventListener('ws-authorize', () => {
159-
authorization.authorize(props.loginUrl, props.clientId, props.businessPartnerId);
171+
this.constructor.authorization.authorize(props.loginUrl, props.clientId, props.businessPartnerId);
160172
});
161173
// Listen for authentication removal requests
162174
window.addEventListener('ws-unauthorize', () => {
163-
authorization.unauthorize();
175+
this.constructor.authorization.unauthorize();
164176
});
165177
}
166178

tests/ws-header/ws-header.spec.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import {WSHeader} from '../../src/ws-header/ws-header';
44
import {CookieStorage} from '../../src/ws-header/storage/cookie-storage';
55
import {LocalStorage} from '../../src/ws-header/storage/local-storage';
66

7-
function clearStorage() {
8-
WSHeader.storage.remove('access_token');
9-
WSHeader.storage.remove('expires_at');
10-
}
11-
127
describe('A WSHeader', () => {
138

9+
afterEach(() => {
10+
WSHeader.removeAccessToken();
11+
});
12+
1413
it('set\'s the storage type', () => {
1514
WSHeader.storage = null;
1615

@@ -25,26 +24,17 @@ describe('A WSHeader', () => {
2524
expect(WSHeader.storage.name).toBe('asd-');
2625
});
2726

28-
it('get\'s the access token', done => {
27+
it('get\'s the access token', () => {
2928
const tokenName = 'asd';
3029
// Only test the basic function, the general authorization ist tested elsewhere
3130
WSHeader.storage.set('access_token', tokenName);
32-
WSHeader.getAccessToken()
33-
.then(accessToken => {
34-
expect(accessToken).toBe(tokenName);
35-
WSHeader.storage.remove('access_token');
36-
done();
37-
});
31+
expect(WSHeader.getAccessToken()).toBe(tokenName);
3832
});
3933

40-
it('doesn\'t get an access token', done => {
34+
it('doesn\'t get an access token', () => {
4135
const tokenName = 'asd';
4236
// Only test the basic function, the general authorization ist tested elsewhere
43-
WSHeader.getAccessToken(`test&no_token=${tokenName}&tests`)
44-
.then(accessToken => {
45-
expect(accessToken).toBeFalsy();
46-
done();
47-
});
37+
expect(WSHeader.getAccessToken(`test&no_token=${tokenName}&tests`)).toBeFalsy();
4838
});
4939

5040
it('initializes correctly', () => {
@@ -55,10 +45,12 @@ describe('A WSHeader', () => {
5545
clientId: 444
5646
});
5747

58-
expect(header.authorization).toBeTruthy();
59-
expect(header.authorization.loginUrl).toBe('1111');
60-
expect(header.authorization.businessPartnerId).toBe('333');
61-
expect(header.authorization.clientId).toBe(444);
48+
spyOn(WSHeader.authorization, 'authorize');
49+
50+
expect(WSHeader.authorization).toBeTruthy();
51+
window.dispatchEvent(new CustomEvent('ws-authorize'));
52+
53+
expect(WSHeader.authorization.authorize).toHaveBeenCalledWith('1111', 444, '333');
6254
expect(header.state.locale).toBe('de');
6355
});
6456

0 commit comments

Comments
 (0)