Uma página só para um papel
Tradução automática
Esta página foi traduzida automaticamente do inglês e ainda não foi revisada por um falante nativo; correções são bem-vindas no GitHub. Se algo não bater, vale o original em inglês. O código e os resultados são os mesmos do original, importados dos arquivos testados.
Tarefa: mostrar uma página só para administradores.
yaml
# Recipe: a page only the admins see; everyone else gets 403, and a visitor
# who is not signed in is sent to sign in.
paths:
components: ./components1
2
3
4
2
3
4
require_auth="true" pede uma sessão de quem entrou; require_role, um dos papéis listados em session.userRole (vários são separados por vírgulas, require_role="admin,editor"):
xml
<q:component name="Reports" require_auth="true" require_role="admin">
<h1>Reports</h1>
<p>Only admins see this, {session.userName}.</p>
</q:component>1
2
3
4
2
3
4
Sem sessão, a resposta redireciona para /login (mude com security.login_url):
xml
<q:component name="Login">
<h1>Sign in</h1>
</q:component>1
2
3
2
3
test:as faz a sessão do teste entrar com um papel, sem senha:
xml
<q:test name="an admin sees the page" page="/reports">
<test:as user="ana" role="admin" />
<test:visit />
<test:expect status="200" text="Only admins see this, ana." />
</q:test>
<q:test name="a member is refused with 403" page="/reports">
<test:as user="bruno" role="member" />
<test:visit />
<test:expect status="403" />
</q:test>
<q:test name="a visitor who is not signed in is sent to sign in" page="/reports">
<test:visit />
<test:expect status="302" redirect="/login" />
</q:test>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
text
tests/reports.test.q
PASS an admin sees the page
PASS a member is refused with 403
PASS a visitor who is not signed in is sent to sign in
3 passed, 0 failed1
2
3
4
5
2
3
4
5