KScript documentazione
IT

Lingua

Tradotto automaticamente dall’inglese, che è la versione originale.

Test

Testare chi può fare cosa

@acting_as() da KScript.Auth imposta chi @user(), @can() e @authorize() considerano, fino al termine del test. Passa null per agire come ospite. Le route che il test chiama con @api(/...) vedono lo stesso utente, incluse le route protette da middleware auth, auth:session o auth:token, quindi una route riservata agli utenti autenticati non richiede un token nel test:

<import package="KScript.Auth" />
<import package="KScript.Net" />

<test name="the dashboard greets me">
  <do>@acting_as(@register('ada@example.test', 'correct-horse-battery-9'))</do>
  <expect value="@api(/dashboard).status" equals="200" />
</test>
import KScript.Auth
import KScript.Net

test(name: 'the dashboard greets me') {
  @acting_as(@register('ada@example.test', 'correct-horse-battery-9'))
  expect(@api(/dashboard).status, equals: 200)
}

Per testare comportamenti basati sui cookie, come la bacheca di un ospite o una preferenza memorizzata, invia tu il cookie: @api(/, options: { headers: { Cookie: 'sb_guest=0f8f' } }). I .cookies della risposta mostrano i cookie impostati.

<import package="KScript.Auth" />
<import package="KScript.Data" />
<import path="../models/post.kscript" />

<test name="only the author may update a post">
  <let name="post" value="Post->create(title: 'Mine', author_id: 1)" />

  <do>@acting_as({ id: 1 })</do>
  <expect value="@can(update, $post)" equals="true" />

  <do>@acting_as({ id: 2 })</do>
  <expect value="@can(update, $post)" equals="false" />
</test>
import KScript.Auth
import KScript.Data
import "../models/post.kscript"

test(name: 'only the author may update a post') {
  $post = Post->create(title: 'Mine', author_id: 1)

  @acting_as({ id: 1 })
  expect(@can(update, $post), equals: true)

  @acting_as({ id: 2 })
  expect(@can(update, $post), equals: false)
}

Edit this page on GitHub