4

私は次のように注釈を付けているコントローラーメソッドを持っています:

@Secured(['ROLE_ADMIN'])
def save() {
    ... // code ommitted
}

管理者ユーザーのみが URL にアクセスできることを確認する単体テストを作成しようとしています。

def "Only the admin user should be able to invoke save"() {
    given:
    def user = createNonAdminUser() // let's pretend this method exists
    controller.springSecurityService = Mock(SpringSecurityService)
    controller.springSecurityService.currentUser >> user

    when:
    controller.save()

    then:
    view ==~ 'accessdenied'
}

ただし、返されるビューはsaveビューであり、アクセスが拒否されたビューではありません。@Secured注釈を完全にバイパスしているようです。@Secured単体テストまたは統合テストからアノテーションをテストする方法はありますか?

4

2 に答える 2

2

これを試して:

SpringSecurityUtils.doWithAuth('superuser') {
    controller.save()
}

http://greybeardedgeek.net/2011/05/13/testing-grails-controllers-with-spock/

于 2013-05-31T04:38:46.533 に答える