diff --git a/app/my-dashboard/header-dashboard/header-dashboard.spec.js b/app/my-dashboard/header-dashboard/header-dashboard.spec.js index ea69e934a..d88fb4ed5 100644 --- a/app/my-dashboard/header-dashboard/header-dashboard.spec.js +++ b/app/my-dashboard/header-dashboard/header-dashboard.spec.js @@ -3,10 +3,7 @@ const mockData = require('../../../tests/test-helpers/mock-data') /* jshint -W117, -W030 */ describe('Header Dashboard Controller', function() { - var userService, profileService, identity var profile = mockData.getMockProfile() - var stats = mockData.getMockStats() - var financials = mockData.getMockUserFinancials() beforeEach(function() { bard.appModule('topcoder') @@ -14,45 +11,7 @@ describe('Header Dashboard Controller', function() { '$controller', '$rootScope', '$q', - 'UserService', - 'ProfileService', 'Helpers') - - userService = UserService - profileService = ProfileService - - identity = function() { - return { - handle: 'albertwang', - userId: 123456 - } - } - - // mock user api - sinon.stub(userService, 'getUserIdentity', function() { - return { - userId: 1234567, - handle: 'ut', - email: 'ut@topcoder.com' - } - }) - - // mock profile api - sinon.stub(profileService, 'getUserProfile', function(handle) { - var deferred = $q.defer() - deferred.resolve(profile) - return deferred.promise - }) - sinon.stub(profileService, 'getUserStats', function(handle) { - var deferred = $q.defer() - deferred.resolve(stats) - return deferred.promise - }) - sinon.stub(profileService, 'getUserFinancials', function(handle) { - var deferred = $q.defer() - deferred.resolve(financials) - return deferred.promise - }) }) bard.verifyNoOutstandingHttpRequests() @@ -61,33 +20,6 @@ describe('Header Dashboard Controller', function() { var controller = null beforeEach( function(){ controller = $controller('HeaderDashboardController', { - UserService : userService, - ProfileService: profileService, - userIdentity: identity, - profile: profile - }) - $rootScope.$apply() - }) - - it('variables should be initialized to correct value', function() { - expect(controller.profile).to.exist - expect(controller.profile.handle).to.equal('albertwang') - }) - }) - - describe('inialization with profile api stats endpoint error', function() { - var controller = null - beforeEach( function(){ - profileService.getUserStats.restore() - sinon.stub(profileService, 'getUserStats', function(handle) { - var deferred = $q.defer() - deferred.reject('failed') - return deferred.promise - }) - controller = $controller('HeaderDashboardController', { - UserService : userService, - ProfileService: profileService, - userIdentity: identity, profile: profile }) $rootScope.$apply() @@ -99,27 +31,17 @@ describe('Header Dashboard Controller', function() { }) }) - describe('inialization with profile api profile endpoint error', function() { + describe('inialization with null profile', function() { var controller = null beforeEach( function(){ - profileService.getUserProfile.restore() - sinon.stub(profileService, 'getUserProfile', function(handle) { - var deferred = $q.defer() - deferred.reject('failed') - return deferred.promise - }) controller = $controller('HeaderDashboardController', { - UserService : userService, - ProfileService: profileService, - userIdentity: identity, - profile: profileService.getUserProfile() + profile: null }) $rootScope.$apply() }) it('variables should be initialized to correct value', function() { - expect(controller.profile.$$state.status).to.equal(2) - expect(controller.profile.$$state.value).to.equal('failed') + expect(controller.profile).to.be.null }) }) diff --git a/app/services/externalAccounts.service.js b/app/services/externalAccounts.service.js index 7e2417ccc..f4ac09a1f 100644 --- a/app/services/externalAccounts.service.js +++ b/app/services/externalAccounts.service.js @@ -194,7 +194,7 @@ import _ from 'lodash' } else { logger.error('Unsupported social login backend', provider) - $q.reject({ + reject({ status: 'failed', 'error': 'Unsupported social login backend \'' + provider + '\'' }) diff --git a/app/services/externalAccounts.service.spec.js b/app/services/externalAccounts.service.spec.js index 662a7116d..dc6b10926 100644 --- a/app/services/externalAccounts.service.spec.js +++ b/app/services/externalAccounts.service.spec.js @@ -14,7 +14,7 @@ describe('ExternalAccount Service', function() { beforeEach(function() { bard.appModule('topcoder') - bard.inject(this, 'ExternalAccountService', '$httpBackend', '$q', 'CONSTANTS', 'JwtInterceptorService', 'auth', 'UserService') + bard.inject(this, 'ExternalAccountService', '$httpBackend', '$q', 'CONSTANTS', 'JwtInterceptorService', 'auth', 'UserService', '$rootScope') bard.mockService(JwtInterceptorService, { getToken: $q.when('token'), _default: $q.when([]) @@ -61,179 +61,186 @@ describe('ExternalAccount Service', function() { }) - afterEach(function() { - $httpBackend.verifyNoOutstandingExpectation() - $httpBackend.verifyNoOutstandingRequest() - }) - - it('service should be defined', function() { - expect(service).to.be.defined - }) - - it('should return linked external accounts', function() { - service.getLinkedAccounts(111).then(function(data) { - expect(data).to.have.length(5) - }) - $httpBackend.flush() - }) - - it('should return linked external accounts data', function() { - service.getAccountsData('test1').then(function(data) { - data = data.plain() - expect(data).to.be.defined - expect(_.keys(data)).to.include.members(['dribbble', 'github', 'behance', 'bitbucket', 'linkedin', 'stackoverflow', 'twitter']) - }) - $httpBackend.flush() - }) - - it('should return all non-pending external links', function() { - // spy - service.getAllExternalLinks('test1', 111, false).then(function(data) { - expect(data).to.be.defined - expect(_.map(data, 'provider')).to.include.members(['dribbble', 'github','bitbucket', 'stackoverflow']) - expect(_.every(_.map(data, 'data'))).to.be.truthy - }) - $httpBackend.flush() - }) - - it('should return all external links including pending', function() { - // spy - service.getAllExternalLinks('test1', 111, true).then(function(data) { - expect(data).to.be.defined - expect(_.map(data, 'provider')).to.include.members(['dribbble', 'github', 'behance', 'bitbucket','stackoverflow']) - expect(data).to.have.length(5) - var nullAccounts = _.remove(data, function(n) {return n.data.status === 'PENDING'}) - expect(nullAccounts).to.have.length(1) + describe('should call api ', function() { + + afterEach(function() { + $httpBackend.verifyNoOutstandingExpectation() + $httpBackend.verifyNoOutstandingRequest() + }) + + it('service should be defined', function() { + expect(service).to.be.defined + }) + + it('should return linked external accounts', function() { + service.getLinkedAccounts(111).then(function(data) { + expect(data).to.have.length(5) + }) + $httpBackend.flush() + }) + + it('should return linked external accounts data', function() { + service.getAccountsData('test1').then(function(data) { + data = data.plain() + expect(data).to.be.defined + expect(_.keys(data)).to.include.members(['dribbble', 'github', 'behance', 'bitbucket', 'linkedin', 'stackoverflow', 'twitter']) + }) + $httpBackend.flush() + }) + + it('should return all non-pending external links', function() { + // spy + service.getAllExternalLinks('test1', 111, false).then(function(data) { + expect(data).to.be.defined + expect(_.map(data, 'provider')).to.include.members(['dribbble', 'github','bitbucket', 'stackoverflow']) + expect(_.every(_.map(data, 'data'))).to.be.truthy + }) + $httpBackend.flush() + }) + + it('should return all external links including pending', function() { + // spy + service.getAllExternalLinks('test1', 111, true).then(function(data) { + expect(data).to.be.defined + expect(_.map(data, 'provider')).to.include.members(['dribbble', 'github', 'behance', 'bitbucket','stackoverflow']) + expect(data).to.have.length(5) + var nullAccounts = _.remove(data, function(n) {return n.data.status === 'PENDING'}) + expect(nullAccounts).to.have.length(1) + }) + $httpBackend.flush() + }) + + it('should not return unsupported links even if they are returned by the API', function() { + var profiles = JSON.parse(JSON.stringify(mockUserLinksData)) + profiles.profiles.push({providerType: 'unsupported'}) + profileGet.respond(200, {result: {content: profiles}}) + // spy + service.getAllExternalLinks('test1', 111, true).then(function(data) { + expect(data).to.be.defined + expect(_.map(data, 'provider')).to.include.members(['dribbble', 'github','bitbucket', 'stackoverflow']) + expect(_.every(_.map(data, 'data'))).to.be.truthy + }) + $httpBackend.flush() + }) + + it('should fail in returning links', function() { + var errorMessage = 'bad request' + // mocks the GET call to respond with 400 bad request + profileGet.respond(400, {result: { status: 400, content: errorMessage } }) + // calls getAllExternalLinks method with valid params + service.getAllExternalLinks('test1', 111, true).then(function(data) { + sinon.assert.fail('should not be called') + }).catch(function(resp) { + expect(resp).to.exist + expect(resp.status).to.exist.to.equal(400) + }) + $httpBackend.flush() + }) + + it('should link external account', function() { + // call linkExternalAccount method with supporte network, should succeed + service.linkExternalAccount('stackoverflow', 'callback').then(function(data) { + expect(data).to.be.defined + // console.log(data) + expect(data.status).to.exist.to.equal('SUCCESS') + expect(data.linkedAccount).to.exist + expect(data.linkedAccount.provider).to.exist.to.equal('stackoverflow') + expect(data.linkedAccount.data).to.exist + expect(data.linkedAccount.data.status).to.exist.to.equal('PENDING') + }) + $httpBackend.flush() + }) + + it('should fail with already existing profile', function() { + var errorMessage = 'social profile exists' + profilePost.respond(400, {result: { status: 400, content: errorMessage } }) + // call linkExternalAccount method, having user service throw already exist + service.linkExternalAccount('stackoverflow', 'callback').then(function(data) { + sinon.assert.fail('should not be called') + }, function(error) { + expect(error).to.be.defined + expect(error.status).to.exist.to.equal('SOCIAL_PROFILE_ALREADY_EXISTS') + expect(error.msg).to.exist.to.equal(errorMessage) + }) + $httpBackend.flush() + }) + + it('should fail with auth0 error', function() { + // call linkExternalAccount method with auth0 throwing error + service.linkExternalAccount('stackoverflow', 'failure').then(function(data) { + sinon.assert.fail('should not be called') + }, function(error) { + expect(error).to.be.exist.to.equal('MOCK_ERROR') + }) + $httpBackend.flush() + }) + + it('should fail, with fatal error, in linking external account', function() { + var errorMessage = 'endpoint not found' + profilePost.respond(404, {result: { status: 404, content: errorMessage } }) + // call unlinkExternalAccount method with supporte network, should succeed + service.linkExternalAccount('stackoverflow', 'callback').then(function(data) { + sinon.assert.fail('should not be called') + }).catch(function(error) { + expect(error).to.be.defined + expect(error.status).to.exist.to.equal('FATAL_ERROR') + expect(error.msg).to.exist.to.equal(errorMessage) + }) + $httpBackend.flush() + }) + + it('should unlink external account', function() { + var errorMessage = 'social profile exists' + profilePost.respond(400, {result: { status: 400, content: errorMessage } }) + // call unlinkExternalAccount method with supporte network, should succeed + service.unlinkExternalAccount('stackoverflow').then(function(data) { + expect(data).to.be.defined + // console.log(data) + expect(data.status).to.exist.to.equal('SUCCESS') + }) + $httpBackend.flush() + }) + + it('should fail, with profile does not exist, in unlinking external account', function() { + var errorMessage = 'social profile does not exists' + profileDelete.respond(404, {result: { status: 404, content: errorMessage } }) + // call unlinkExternalAccount method with supporte network, should succeed + service.unlinkExternalAccount('stackoverflow').then(function(data) { + sinon.assert.fail('should not be called') + }).catch(function(error) { + expect(error).to.be.defined + expect(error.status).to.exist.to.equal('SOCIAL_PROFILE_NOT_EXIST') + expect(error.msg).to.exist.to.equal(errorMessage) + }) + $httpBackend.flush() + }) + + it('should fail, with fatal error, in unlinking external account', function() { + var errorMessage = 'bad request' + profileDelete.respond(400, {result: { status: 400, content: errorMessage } }) + // call unlinkExternalAccount method with supporte network, should succeed + service.unlinkExternalAccount('stackoverflow').then(function(data) { + sinon.assert.fail('should not be called') + }).catch(function(error) { + expect(error).to.be.defined + expect(error.status).to.exist.to.equal('FATAL_ERROR') + expect(error.msg).to.exist.to.equal(errorMessage) + }) + $httpBackend.flush() }) - $httpBackend.flush() - }) - it('should not return unsupported links even if they are returned by the API', function() { - var profiles = JSON.parse(JSON.stringify(mockUserLinksData)) - profiles.profiles.push({providerType: 'unsupported'}) - profileGet.respond(200, {result: {content: profiles}}) - // spy - service.getAllExternalLinks('test1', 111, true).then(function(data) { - expect(data).to.be.defined - expect(_.map(data, 'provider')).to.include.members(['dribbble', 'github','bitbucket', 'stackoverflow']) - expect(_.every(_.map(data, 'data'))).to.be.truthy - }) - $httpBackend.flush() - }) - - it('should fail in returning links', function() { - var errorMessage = 'bad request' - // mocks the GET call to respond with 400 bad request - profileGet.respond(400, {result: { status: 400, content: errorMessage } }) - // calls getAllExternalLinks method with valid params - service.getAllExternalLinks('test1', 111, true).then(function(data) { - sinon.assert.fail('should not be called') - }).catch(function(resp) { - expect(resp).to.exist - expect(resp.status).to.exist.to.equal(400) - }) - $httpBackend.flush() - }) - - it('should link external account', function() { - // call linkExternalAccount method with supporte network, should succeed - service.linkExternalAccount('stackoverflow', 'callback').then(function(data) { - expect(data).to.be.defined - // console.log(data) - expect(data.status).to.exist.to.equal('SUCCESS') - expect(data.linkedAccount).to.exist - expect(data.linkedAccount.provider).to.exist.to.equal('stackoverflow') - expect(data.linkedAccount.data).to.exist - expect(data.linkedAccount.data.status).to.exist.to.equal('PENDING') - }) - $httpBackend.flush() }) it('should fail with unsupported network', function() { // call linkExternalAccount method with unsupported network, should fail - service.linkExternalAccount('unsupported', 'callback').then(function(data) { - expect(data).to.be.defined - expect(data.status).to.exist.to.equal('failed') - expect(data.error.to.contain('unsupported')) - }) - }) - - it('should fail with already existing profile', function() { - var errorMessage = 'social profile exists' - profilePost.respond(400, {result: { status: 400, content: errorMessage } }) - // call linkExternalAccount method, having user service throw already exist - service.linkExternalAccount('stackoverflow', 'callback').then(function(data) { - sinon.assert.fail('should not be called') - }, function(error) { - expect(error).to.be.defined - expect(error.status).to.exist.to.equal('SOCIAL_PROFILE_ALREADY_EXISTS') - expect(error.msg).to.exist.to.equal(errorMessage) - }) - $httpBackend.flush() - }) - - it('should fail with auth0 error', function() { - // call linkExternalAccount method with auth0 throwing error - service.linkExternalAccount('stackoverflow', 'failure').then(function(data) { - sinon.assert.fail('should not be called') - }, function(error) { - expect(error).to.be.exist.to.equal('MOCK_ERROR') - }) - $httpBackend.flush() - }) - - it('should fail, with fatal error, in linking external account', function() { - var errorMessage = 'endpoint not found' - profilePost.respond(404, {result: { status: 404, content: errorMessage } }) - // call unlinkExternalAccount method with supporte network, should succeed - service.linkExternalAccount('stackoverflow', 'callback').then(function(data) { - sinon.assert.fail('should not be called') - }).catch(function(error) { - expect(error).to.be.defined - expect(error.status).to.exist.to.equal('FATAL_ERROR') - expect(error.msg).to.exist.to.equal(errorMessage) - }) - $httpBackend.flush() - }) - - it('should unlink external account', function() { - var errorMessage = 'social profile exists' - profilePost.respond(400, {result: { status: 400, content: errorMessage } }) - // call unlinkExternalAccount method with supporte network, should succeed - service.unlinkExternalAccount('stackoverflow').then(function(data) { - expect(data).to.be.defined - // console.log(data) - expect(data.status).to.exist.to.equal('SUCCESS') - }) - $httpBackend.flush() - }) - - it('should fail, with profile does not exist, in unlinking external account', function() { - var errorMessage = 'social profile does not exists' - profileDelete.respond(404, {result: { status: 404, content: errorMessage } }) - // call unlinkExternalAccount method with supporte network, should succeed - service.unlinkExternalAccount('stackoverflow').then(function(data) { + var promise = service.linkExternalAccount('unsupported', 'callback') + promise.then(function(data) { sinon.assert.fail('should not be called') - }).catch(function(error) { - expect(error).to.be.defined - expect(error.status).to.exist.to.equal('SOCIAL_PROFILE_NOT_EXIST') - expect(error.msg).to.exist.to.equal(errorMessage) - }) - $httpBackend.flush() - }) - - it('should fail, with fatal error, in unlinking external account', function() { - var errorMessage = 'bad request' - profileDelete.respond(400, {result: { status: 400, content: errorMessage } }) - // call unlinkExternalAccount method with supporte network, should succeed - service.unlinkExternalAccount('stackoverflow').then(function(data) { - sinon.assert.fail('should not be called') - }).catch(function(error) { - expect(error).to.be.defined - expect(error.status).to.exist.to.equal('FATAL_ERROR') - expect(error.msg).to.exist.to.equal(errorMessage) + }, function(resp) { + expect(resp).to.exist + expect(resp.status).to.exist.to.equal('failed') }) - $httpBackend.flush() + $rootScope.$apply() }) })