Skip to content

Commit 15023ea

Browse files
Merge pull request #51 from abuecker/fix/profile
fix(profile): Add broadcast “oauth:profile” once profile is retrieved.
2 parents 5955852 + 1a1243b commit 15023ea

File tree

3 files changed

+129
-108
lines changed

3 files changed

+129
-108
lines changed

app/scripts/services/profile.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
var profileClient = angular.module('oauth.profile', [])
44

5-
profileClient.factory('Profile', function($http, AccessToken) {
5+
profileClient.factory('Profile', function($http, AccessToken, $rootScope) {
66
var service = {};
77
var profile;
88

99
service.find = function(uri) {
1010
var promise = $http.get(uri, { headers: headers() });
11-
promise.success(function(response) { profile = response });
11+
promise.success(function(response) {
12+
profile = response;
13+
$rootScope.$broadcast('oauth:profile', profile);
14+
});
1215
return promise;
1316
};
1417

dist/oauth-ng.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,16 @@ endpointClient.factory('Endpoint', function(AccessToken, $location) {
240240

241241
var profileClient = angular.module('oauth.profile', [])
242242

243-
profileClient.factory('Profile', function($http, AccessToken) {
243+
profileClient.factory('Profile', function($http, AccessToken, $rootScope) {
244244
var service = {};
245245
var profile;
246246

247247
service.find = function(uri) {
248248
var promise = $http.get(uri, { headers: headers() });
249-
promise.success(function(response) { profile = response });
249+
promise.success(function(response) {
250+
profile = response;
251+
$rootScope.$broadcast('oauth:profile', profile);
252+
});
250253
return promise;
251254
};
252255

test/spec/services/profile.js

Lines changed: 119 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,120 @@
1-
//'use strict';
1+
'use strict';
22

3-
//describe('Profile', function() {
4-
5-
//var $rootScope, $location, $httpBackend, $http, AccessToken, Profile;
6-
//var result, date, callback;
7-
8-
//var fragment = 'access_token=token&token_type=bearer&expires_in=7200&state=/path';
9-
//var headers = { 'Accept': 'application/json, text/plain, */*', 'Authorization': 'Bearer token' }
10-
//var params = { site: 'http://example.com', client: 'client-id', redirect: 'http://example.com/redirect', scope: 'scope', profileUri: 'http://example.com/me' };
11-
//var resource = { id: '1', name: 'Alice' };
12-
13-
//beforeEach(module('oauth'));
14-
15-
//beforeEach(inject(function($injector) { $rootScope = $injector.get('$rootScope') }));
16-
//beforeEach(inject(function($injector) { $location = $injector.get('$location') }));
17-
//beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend') }));
18-
//beforeEach(inject(function($injector) { $http = $injector.get('$http') }));
19-
//beforeEach(inject(function($injector) { AccessToken = $injector.get('AccessToken') }));
20-
//beforeEach(inject(function($injector) { Profile = $injector.get('Profile') }));
21-
22-
//beforeEach(function() { callback = jasmine.createSpy('callback') });
23-
24-
25-
//describe('.get', function() {
26-
27-
//describe('when authenticated', function() {
28-
29-
//beforeEach(function() {
30-
//$location.hash(fragment);
31-
//AccessToken.set(params);
32-
//});
33-
34-
//beforeEach(function() {
35-
//$httpBackend.whenGET('http://example.com/me', headers).respond(resource);
36-
//});
37-
38-
//describe('when gets the profile', function() {
39-
40-
//it('makes the request', function() {
41-
//$httpBackend.expect('GET', 'http://example.com/me');
42-
//Profile.find(params.profileUri);
43-
//$rootScope.$apply();
44-
//$httpBackend.flush();
45-
//});
46-
47-
//it('gets the resource', inject(function(Profile) {
48-
//Profile.find(params.profileUri).success(function(response) { result = response });
49-
//$rootScope.$apply();
50-
//$httpBackend.flush();
51-
//expect(result.name).toEqual('Alice');
52-
//}));
53-
54-
//it('caches the profile', function() {
55-
//Profile.find(params.profileUri);
56-
//$rootScope.$apply();
57-
//$httpBackend.flush();
58-
//expect(Profile.get().name).toEqual('Alice');
59-
//});
60-
61-
//describe('when expired', function() {
62-
63-
//beforeEach(function() {
64-
//$rootScope.$on('oauth:expired', callback);
65-
//});
66-
67-
//beforeEach(function() {
68-
//date = new Date();
69-
//date.setTime(date.getTime() + 86400000);
70-
//});
71-
72-
//beforeEach(function() {
73-
//Timecop.install();
74-
//Timecop.travel(date); // go one day in the future
75-
//});
76-
77-
//afterEach(function() {
78-
//Timecop.uninstall();
79-
//});
80-
81-
//it('fires the oauth:expired event', inject(function(Profile) {
82-
//Profile.find(params.profileUri);
83-
//$rootScope.$apply();
84-
//$httpBackend.flush();
85-
//var event = jasmine.any(Object);
86-
//var token = jasmine.any(Object);
87-
//expect(callback).toHaveBeenCalledWith(event, token);
88-
//}));
89-
//});
90-
//});
91-
92-
93-
//describe('when sets the profile', function() {
94-
95-
//beforeEach(function() {
96-
//Profile.set(resource);
97-
//});
98-
99-
//it('caches the profile', function() {
100-
//expect(Profile.get().name).toEqual('Alice');
101-
//});
102-
//});
103-
//});
104-
//});
105-
//});
3+
describe('Profile', function() {
4+
5+
var $rootScope, $location, $httpBackend, $http, AccessToken, Profile;
6+
var result, date, callback;
7+
8+
var fragment = 'access_token=token&token_type=bearer&expires_in=7200&state=/path';
9+
var headers = { 'Accept': 'application/json, text/plain, */*', 'Authorization': 'Bearer token' }
10+
var params = { site: 'http://example.com', client: 'client-id', redirect: 'http://example.com/redirect', scope: 'scope', profileUri: 'http://example.com/me' };
11+
var resource = { id: '1', name: 'Alice' };
12+
13+
beforeEach(module('oauth'));
14+
15+
beforeEach(inject(function($injector) { $rootScope = $injector.get('$rootScope') }));
16+
beforeEach(inject(function($injector) { $location = $injector.get('$location') }));
17+
beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend') }));
18+
beforeEach(inject(function($injector) { $http = $injector.get('$http') }));
19+
beforeEach(inject(function($injector) { AccessToken = $injector.get('AccessToken') }));
20+
beforeEach(inject(function($injector) { Profile = $injector.get('Profile') }));
21+
22+
beforeEach(function() { callback = jasmine.createSpy('callback') });
23+
24+
25+
describe('.get', function() {
26+
27+
describe('when authenticated', function() {
28+
29+
beforeEach(function() {
30+
$location.hash(fragment);
31+
AccessToken.set(params);
32+
});
33+
34+
beforeEach(function() {
35+
$httpBackend.whenGET('http://example.com/me', headers).respond(resource);
36+
});
37+
38+
describe('when gets the profile', function() {
39+
40+
it('makes the request', function() {
41+
$httpBackend.expect('GET', 'http://example.com/me');
42+
Profile.find(params.profileUri);
43+
$rootScope.$apply();
44+
$httpBackend.flush();
45+
});
46+
47+
it('gets the resource', inject(function(Profile) {
48+
Profile.find(params.profileUri).success(function(response) { result = response });
49+
$rootScope.$apply();
50+
$httpBackend.flush();
51+
expect(result.name).toEqual('Alice');
52+
}));
53+
54+
it('caches the profile', function() {
55+
Profile.find(params.profileUri);
56+
$rootScope.$apply();
57+
$httpBackend.flush();
58+
expect(Profile.get().name).toEqual('Alice');
59+
});
60+
61+
it('fires oauth:profile event', function(done) {
62+
63+
$rootScope.$on('oauth:profile', function (event, profile) {
64+
expect(typeof profile).toBe('object');
65+
done();
66+
});
67+
68+
Profile.find(params.profileUri);
69+
$rootScope.$apply();
70+
$httpBackend.flush();
71+
72+
});
73+
74+
75+
describe('when expired', function() {
76+
77+
beforeEach(function() {
78+
$rootScope.$on('oauth:expired', callback);
79+
});
80+
81+
beforeEach(function() {
82+
date = new Date();
83+
date.setTime(date.getTime() + 86400000);
84+
});
85+
86+
beforeEach(function() {
87+
Timecop.install();
88+
Timecop.travel(date); // go one day in the future
89+
});
90+
91+
afterEach(function() {
92+
Timecop.uninstall();
93+
});
94+
95+
it('fires the oauth:expired event', inject(function(Profile) {
96+
Profile.find(params.profileUri);
97+
$rootScope.$apply();
98+
$httpBackend.flush();
99+
var event = jasmine.any(Object);
100+
var token = jasmine.any(Object);
101+
expect(callback).toHaveBeenCalledWith(event, token);
102+
}));
103+
});
104+
});
105+
106+
107+
describe('when sets the profile', function() {
108+
109+
beforeEach(function() {
110+
Profile.set(resource);
111+
});
112+
113+
it('caches the profile', function() {
114+
expect(Profile.get().name).toEqual('Alice');
115+
});
116+
117+
});
118+
});
119+
});
120+
});

0 commit comments

Comments
 (0)