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

View File

@ -44,6 +44,15 @@ describe "ProfileController", ->
provide.value "$routeParams", mocks.routeParams provide.value "$routeParams", mocks.routeParams
_mockXhrErrorService = () ->
stub = sinon.stub()
mocks.xhrErrorService = {
response: sinon.spy()
}
provide.value "tgXhrErrorService", mocks.xhrErrorService
_mocks = () -> _mocks = () ->
module ($provide) -> module ($provide) ->
provide = $provide provide = $provide
@ -51,6 +60,7 @@ describe "ProfileController", ->
_mockCurrentUser() _mockCurrentUser()
_mockRouteParams() _mockRouteParams()
_mockUserService() _mockUserService()
_mockXhrErrorService()
return null return null
@ -86,6 +96,25 @@ describe "ProfileController", ->
done() 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", () -> it "define current user", () ->
$scope = $rootScope.$new() $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