Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions io.github.cl-sdk.wst.session.csrf.test.asd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(asdf:defsystem #:io.github.cl-sdk.wst.session.csrf.test
:description "Tests for io.github.cl-sdk.wst.session.csrf."
:author "Bruno Dias"
:license "Unlicense"
:version "0.1.0"
:depends-on (#:io.github.cl-sdk.wst.test.support
#:io.github.cl-sdk.wst.session.csrf)
:pathname "t"
:serial t
:components ((:file "session-csrf-tests")))
1 change: 1 addition & 0 deletions io.github.cl-sdk.wst.test.asd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#:io.github.cl-sdk.wst.routing.response.dsl.test
#:io.github.cl-sdk.wst.routing.test
#:io.github.cl-sdk.wst.routing.woo.test
#:io.github.cl-sdk.wst.session.csrf.test
#:io.github.cl-sdk.wst.session.sqlite.test
#:io.github.cl-sdk.wst.trace-context.routing.test
#:io.github.cl-sdk.wst.trace-context.test))
51 changes: 51 additions & 0 deletions t/session-csrf-tests.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(defpackage #:io.github.cl-sdk.wst.session.csrf.test
(:use #:cl #:fiveam #:io.github.cl-sdk.wst.session.csrf))

(in-package #:io.github.cl-sdk.wst.session.csrf.test)

(def-suite session-csrf-suite
:description "Tests for the wst.session.csrf package.")

(in-suite session-csrf-suite)

(defclass test-csrf-context ()
((csrf-token :initform nil
:accessor test-csrf-context-token)))

(defmethod session-csrf-token ((obj test-csrf-context) &key &allow-other-keys)
(test-csrf-context-token obj))

(defmethod add-session-csrf-token ((obj test-csrf-context) key &key &allow-other-keys)
(setf (test-csrf-context-token obj) key))

(defmethod remove-session-csrf-token ((obj test-csrf-context) &key &allow-other-keys)
(setf (test-csrf-context-token obj) nil))

(defmethod verify-session-csrf-token ((obj test-csrf-context) key &key &allow-other-keys)
(let ((stored (test-csrf-context-token obj)))
(and stored key (string= stored key))))

(5am:def-test session-csrf-token-lifecycle ()
(let ((context (make-instance 'test-csrf-context)))
(5am:is-false (session-csrf-token context))
(5am:is-false (verify-session-csrf-token context "csrf-1"))

(5am:is (string= "csrf-1"
(add-session-csrf-token context "csrf-1")))
(5am:is (string= "csrf-1"
(session-csrf-token context)))
(5am:is-true (verify-session-csrf-token context "csrf-1"))
(5am:is-false (verify-session-csrf-token context "csrf-2"))

(5am:is-false (remove-session-csrf-token context))
(5am:is-false (session-csrf-token context))
(5am:is-false (verify-session-csrf-token context "csrf-1"))))

(5am:def-test session-csrf-token-can-be-replaced ()
(let ((context (make-instance 'test-csrf-context)))
(add-session-csrf-token context "csrf-old")
(5am:is-true (verify-session-csrf-token context "csrf-old"))

(add-session-csrf-token context "csrf-new")
(5am:is-false (verify-session-csrf-token context "csrf-old"))
(5am:is-true (verify-session-csrf-token context "csrf-new"))))
Loading