Issue #2779 - 404 non-existent user

stable
Juanfran 2015-06-01 12:47:39 +02:00
parent 656858a95a
commit 9d11dd407d
4 changed files with 130 additions and 3 deletions

View File

@ -1,12 +1,13 @@
class ProfilePageController extends taiga.Controller
class ProfilePageController
@.$inject = [
"$appTitle",
"tgCurrentUserService",
"$routeParams",
"tgUserService"
"tgUserService",
"tgXhrErrorService"
]
constructor: (@appTitle, @currentUserService, @routeParams, @userService) ->
constructor: (@appTitle, @currentUserService, @routeParams, @userService, @xhrError) ->
@.isCurrentUser = false
if @routeParams.slug
@ -16,6 +17,9 @@ class ProfilePageController extends taiga.Controller
@.user = user
@.isCurrentUser = false
@appTitle.set(@.user.get('full_name'))
.catch (xhr) =>
@xhrError.response(xhr)
else
@.user = @currentUserService.getUser()
@.isCurrentUser = true

View File

@ -44,6 +44,15 @@ describe "ProfileController", ->
provide.value "$routeParams", mocks.routeParams
_mockXhrErrorService = () ->
stub = sinon.stub()
mocks.xhrErrorService = {
response: sinon.spy()
}
provide.value "tgXhrErrorService", mocks.xhrErrorService
_mocks = () ->
module ($provide) ->
provide = $provide
@ -51,6 +60,7 @@ describe "ProfileController", ->
_mockCurrentUser()
_mockRouteParams()
_mockUserService()
_mockXhrErrorService()
return null
@ -86,6 +96,25 @@ describe "ProfileController", ->
done()
)
it "non-existent user", (done) ->
$scope = $rootScope.$new()
mocks.routeParams.slug = "user-slug"
xhr = {
status: 404
}
mocks.userService.getUserByUserName.withArgs(mocks.routeParams.slug).promise().reject(xhr)
ctrl = $controller("Profile")
setTimeout ( ->
expect(mocks.xhrErrorService.response.withArgs(xhr)).to.be.calledOnce
done()
)
it "define current user", () ->
$scope = $rootScope.$new()

View File

@ -0,0 +1,21 @@
class xhrError extends taiga.Service
@.$inject = [
"$q",
"$location",
"$tgNavUrls"
]
constructor: (@q, @location, @navUrls) ->
response: (xhr) ->
if xhr
if xhr.status == 404
@location.path(@navUrls.resolve("not-found"))
@location.replace()
else if xhr.status == 403
@location.path(@navUrls.resolve("permission-denied"))
@location.replace()
return @q.reject(xhr)
angular.module("taigaCommon").service("tgXhrErrorService", xhrError)

View File

@ -0,0 +1,73 @@
describe "tgXhrErrorService", ->
xhrErrorService = provide = null
mocks = {}
_mockQ = () ->
mocks.q = {
reject: sinon.spy()
}
provide.value "$q", mocks.q
_mockLocation = () ->
mocks.location = {
path: sinon.spy(),
replace: sinon.spy()
}
provide.value "$location", mocks.location
_mockNavUrls = () ->
mocks.navUrls = {
resolve: sinon.stub()
}
provide.value "$tgNavUrls", mocks.navUrls
_inject = (callback) ->
inject (_tgXhrErrorService_) ->
xhrErrorService = _tgXhrErrorService_
callback() if callback
_mocks = () ->
module ($provide) ->
provide = $provide
_mockQ()
_mockLocation()
_mockNavUrls()
return null
_setup = ->
_mocks()
beforeEach ->
module "taigaCommon"
_setup()
_inject()
it "404 status redirect to not-found page", () ->
xhr = {
status: 404
}
mocks.navUrls.resolve.withArgs("not-found").returns("not-found")
xhrErrorService.response(xhr)
expect(mocks.q.reject.withArgs(xhr)).to.be.calledOnce
expect(mocks.location.path.withArgs("not-found")).to.be.calledOnce
expect(mocks.location.replace).to.be.calledOnce
it "403 status redirect to permission-denied page", () ->
xhr = {
status: 403
}
mocks.navUrls.resolve.withArgs("permission-denied").returns("permission-denied")
xhrErrorService.response(xhr)
expect(mocks.q.reject.withArgs(xhr)).to.be.calledOnce
expect(mocks.location.path.withArgs("permission-denied")).to.be.calledOnce
expect(mocks.location.replace).to.be.calledOnce