Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix(github): add github token handling and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgutride authored and joshuawilson committed Dec 13, 2017
1 parent b0d0449 commit 7b4522c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/app/auth/authentication.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,57 @@ describe('Service: Authentication service', () => {
expect(localStorage.getItem('openshift-v3_token')).toBeNull();
});
}));

it('Github token processing', (done) => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: tokenJson,
status: 201
})
));
});
spyOn(authenticationService, 'setupRefreshTimer');

broadcaster.on('loggedin').subscribe((data: number) => {
let token = JSON.parse(tokenJson);
authenticationService.gitHubToken.subscribe(output => {
// then
expect(output == token.access_token);
expect(localStorage.getItem('github_token')).toBe(token.access_token);
authenticationService.logout();
done();
});
});

authenticationService.logIn(tokenJson);
});

it('Github token clear', (done) => {
// given
mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: tokenJson,
status: 201
})
));
});
spyOn(authenticationService, 'setupRefreshTimer');

broadcaster.on('loggedin').subscribe((data: number) => {
expect(localStorage.getItem('github_token')).not.toBeNull();
authenticationService.clearGitHubToken();
});
authenticationService.logIn(tokenJson);

authenticationService.gitHubToken.subscribe(output => {
// token should be empty after token is cleared
expect(output).toBe('');
expect(localStorage.getItem('github_token')).toBeNull();
authenticationService.logout();
done();
});
});
});
13 changes: 12 additions & 1 deletion src/app/auth/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ export class AuthenticationService {
return token;
}

clearGitHubToken(): void {
localStorage.removeItem(this.github + '_token');
this.gitHubToken = Observable.of('');
}

private createFederatedToken(broker: string, processToken: ProcessTokenResponse): Observable<string> {
let res = this.refreshTokens.switchMap(token => {
let headers = new Headers({ 'Content-Type': 'application/json' });
Expand All @@ -167,7 +172,13 @@ export class AuthenticationService {
}
return Observable.of({} as Token);
})
.do(token => localStorage.setItem(broker + '_token', token.access_token))
.do(token => {
if (token.access_token) {
localStorage.setItem(broker + '_token', token.access_token);
} else {
localStorage.removeItem(broker + '_token');
}
})
.map(t => t.access_token);
})
.publishReplay(1);
Expand Down

0 comments on commit 7b4522c

Please sign in to comment.