diff --git a/app/scripts/services/profile.js b/app/scripts/services/profile.js index 671136a..48990bb 100644 --- a/app/scripts/services/profile.js +++ b/app/scripts/services/profile.js @@ -2,13 +2,16 @@ var profileClient = angular.module('oauth.profile', []) -profileClient.factory('Profile', function($http, AccessToken) { +profileClient.factory('Profile', function($http, AccessToken, $rootScope) { var service = {}; var profile; service.find = function(uri) { var promise = $http.get(uri, { headers: headers() }); - promise.success(function(response) { profile = response }); + promise.success(function(response) { + profile = response; + $rootScope.$broadcast('oauth:profile', profile); + }); return promise; }; diff --git a/dist/oauth-ng.js b/dist/oauth-ng.js index 84f02c5..5a2b2b0 100644 --- a/dist/oauth-ng.js +++ b/dist/oauth-ng.js @@ -240,13 +240,16 @@ endpointClient.factory('Endpoint', function(AccessToken, $location) { var profileClient = angular.module('oauth.profile', []) -profileClient.factory('Profile', function($http, AccessToken) { +profileClient.factory('Profile', function($http, AccessToken, $rootScope) { var service = {}; var profile; service.find = function(uri) { var promise = $http.get(uri, { headers: headers() }); - promise.success(function(response) { profile = response }); + promise.success(function(response) { + profile = response; + $rootScope.$broadcast('oauth:profile', profile); + }); return promise; }; diff --git a/test/spec/services/profile.js b/test/spec/services/profile.js index 3eedbe9..f080a4b 100644 --- a/test/spec/services/profile.js +++ b/test/spec/services/profile.js @@ -1,105 +1,120 @@ -//'use strict'; +'use strict'; -//describe('Profile', function() { - - //var $rootScope, $location, $httpBackend, $http, AccessToken, Profile; - //var result, date, callback; - - //var fragment = 'access_token=token&token_type=bearer&expires_in=7200&state=/path'; - //var headers = { 'Accept': 'application/json, text/plain, */*', 'Authorization': 'Bearer token' } - //var params = { site: 'http://example.com', client: 'client-id', redirect: 'http://example.com/redirect', scope: 'scope', profileUri: 'http://example.com/me' }; - //var resource = { id: '1', name: 'Alice' }; - - //beforeEach(module('oauth')); - - //beforeEach(inject(function($injector) { $rootScope = $injector.get('$rootScope') })); - //beforeEach(inject(function($injector) { $location = $injector.get('$location') })); - //beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend') })); - //beforeEach(inject(function($injector) { $http = $injector.get('$http') })); - //beforeEach(inject(function($injector) { AccessToken = $injector.get('AccessToken') })); - //beforeEach(inject(function($injector) { Profile = $injector.get('Profile') })); - - //beforeEach(function() { callback = jasmine.createSpy('callback') }); - - - //describe('.get', function() { - - //describe('when authenticated', function() { - - //beforeEach(function() { - //$location.hash(fragment); - //AccessToken.set(params); - //}); - - //beforeEach(function() { - //$httpBackend.whenGET('http://example.com/me', headers).respond(resource); - //}); - - //describe('when gets the profile', function() { - - //it('makes the request', function() { - //$httpBackend.expect('GET', 'http://example.com/me'); - //Profile.find(params.profileUri); - //$rootScope.$apply(); - //$httpBackend.flush(); - //}); - - //it('gets the resource', inject(function(Profile) { - //Profile.find(params.profileUri).success(function(response) { result = response }); - //$rootScope.$apply(); - //$httpBackend.flush(); - //expect(result.name).toEqual('Alice'); - //})); - - //it('caches the profile', function() { - //Profile.find(params.profileUri); - //$rootScope.$apply(); - //$httpBackend.flush(); - //expect(Profile.get().name).toEqual('Alice'); - //}); - - //describe('when expired', function() { - - //beforeEach(function() { - //$rootScope.$on('oauth:expired', callback); - //}); - - //beforeEach(function() { - //date = new Date(); - //date.setTime(date.getTime() + 86400000); - //}); - - //beforeEach(function() { - //Timecop.install(); - //Timecop.travel(date); // go one day in the future - //}); - - //afterEach(function() { - //Timecop.uninstall(); - //}); - - //it('fires the oauth:expired event', inject(function(Profile) { - //Profile.find(params.profileUri); - //$rootScope.$apply(); - //$httpBackend.flush(); - //var event = jasmine.any(Object); - //var token = jasmine.any(Object); - //expect(callback).toHaveBeenCalledWith(event, token); - //})); - //}); - //}); - - - //describe('when sets the profile', function() { - - //beforeEach(function() { - //Profile.set(resource); - //}); - - //it('caches the profile', function() { - //expect(Profile.get().name).toEqual('Alice'); - //}); - //}); - //}); - //}); -//}); +describe('Profile', function() { + + var $rootScope, $location, $httpBackend, $http, AccessToken, Profile; + var result, date, callback; + + var fragment = 'access_token=token&token_type=bearer&expires_in=7200&state=/path'; + var headers = { 'Accept': 'application/json, text/plain, */*', 'Authorization': 'Bearer token' } + var params = { site: 'http://example.com', client: 'client-id', redirect: 'http://example.com/redirect', scope: 'scope', profileUri: 'http://example.com/me' }; + var resource = { id: '1', name: 'Alice' }; + + beforeEach(module('oauth')); + + beforeEach(inject(function($injector) { $rootScope = $injector.get('$rootScope') })); + beforeEach(inject(function($injector) { $location = $injector.get('$location') })); + beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend') })); + beforeEach(inject(function($injector) { $http = $injector.get('$http') })); + beforeEach(inject(function($injector) { AccessToken = $injector.get('AccessToken') })); + beforeEach(inject(function($injector) { Profile = $injector.get('Profile') })); + + beforeEach(function() { callback = jasmine.createSpy('callback') }); + + + describe('.get', function() { + + describe('when authenticated', function() { + + beforeEach(function() { + $location.hash(fragment); + AccessToken.set(params); + }); + + beforeEach(function() { + $httpBackend.whenGET('http://example.com/me', headers).respond(resource); + }); + + describe('when gets the profile', function() { + + it('makes the request', function() { + $httpBackend.expect('GET', 'http://example.com/me'); + Profile.find(params.profileUri); + $rootScope.$apply(); + $httpBackend.flush(); + }); + + it('gets the resource', inject(function(Profile) { + Profile.find(params.profileUri).success(function(response) { result = response }); + $rootScope.$apply(); + $httpBackend.flush(); + expect(result.name).toEqual('Alice'); + })); + + it('caches the profile', function() { + Profile.find(params.profileUri); + $rootScope.$apply(); + $httpBackend.flush(); + expect(Profile.get().name).toEqual('Alice'); + }); + + it('fires oauth:profile event', function(done) { + + $rootScope.$on('oauth:profile', function (event, profile) { + expect(typeof profile).toBe('object'); + done(); + }); + + Profile.find(params.profileUri); + $rootScope.$apply(); + $httpBackend.flush(); + + }); + + + describe('when expired', function() { + + beforeEach(function() { + $rootScope.$on('oauth:expired', callback); + }); + + beforeEach(function() { + date = new Date(); + date.setTime(date.getTime() + 86400000); + }); + + beforeEach(function() { + Timecop.install(); + Timecop.travel(date); // go one day in the future + }); + + afterEach(function() { + Timecop.uninstall(); + }); + + it('fires the oauth:expired event', inject(function(Profile) { + Profile.find(params.profileUri); + $rootScope.$apply(); + $httpBackend.flush(); + var event = jasmine.any(Object); + var token = jasmine.any(Object); + expect(callback).toHaveBeenCalledWith(event, token); + })); + }); + }); + + + describe('when sets the profile', function() { + + beforeEach(function() { + Profile.set(resource); + }); + + it('caches the profile', function() { + expect(Profile.get().name).toEqual('Alice'); + }); + + }); + }); + }); +});