Skip to content

Commit

Permalink
Merge pull request #24 from pfitaxel/test-secret
Browse files Browse the repository at this point in the history
Strenghten tests - Add a server "secret" in the test environment
  • Loading branch information
erikmd authored Aug 31, 2021
2 parents 4cd99f5 + 1a14f67 commit 3646c72
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 29 deletions.
2 changes: 2 additions & 0 deletions learn-ocaml.el
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ To be used as a `make-process' sentinel, using args PROC and STRING."
nil
callback)))

;; TODO: Use cl-defun
(defun learn-ocaml-create-token-cmd (nickname secret callback)
"Create a new token for NICKNAME.
Argument SECRET may be needed by the server.
Expand Down Expand Up @@ -979,6 +980,7 @@ Note: this function will be used by `learn-ocaml-login-with-token'."
;; without config file
(learn-ocaml-init-cmd
:server new-server-value
:secret secret
:token new-token-value
:nickname nickname
:callback callback)
Expand Down
3 changes: 1 addition & 2 deletions run_test_backend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ run_server () {
if [ "$USE_PASSWD" = "true" ]; then
cp -f "$PWD/tests/use_passwd.json" "$PWD/tests/repo/server_config.json"
else
# TODO: Add a secret
rm -f "$PWD/tests/repo/server_config.json"
cp -f "$PWD/tests/use_token.json" "$PWD/tests/repo/server_config.json"
fi

# Don't use the "-d" option
Expand Down
1 change: 0 additions & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# These files contain some useful/hidden info for ERT tests.
confirm="$PWD/confirm.txt"
teacher="$PWD/teacher.txt"
# TODO Use them.

# This file contains the Server Container ID (gen by ./run_test_backend.sh)
fcid="$PWD/learn-ocaml-server.pid"
Expand Down
23 changes: 5 additions & 18 deletions tests/002-use-token/runtests.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
:body (lambda ()
(learn-ocaml-create-token-cmd
"Foo"
""
(learn-ocaml-test-secret)
(lambda (token)
(learn-ocaml-use-metadata-cmd
token
Expand All @@ -54,7 +54,7 @@
:body (lambda ()
(learn-ocaml-create-token-cmd
"Foo"
""
(learn-ocaml-test-secret)
(lambda (token)
(learn-ocaml-init
:new-server-value nil
Expand All @@ -75,7 +75,7 @@
(learn-ocaml-init
:new-server-value nil
:nickname "Foo"
:secret ""
:secret (learn-ocaml-test-secret)
:callback (lambda (_)
(learn-ocaml-give-token-cmd
(lambda (token2)
Expand All @@ -86,20 +86,7 @@

;; MOVED TO 001-common:
;; (ert-deftest-async a10_learn-ocaml-on-load-test-another-token-no-config (done)
;; (learn-ocaml-test-run-with
;; :before-action 'login-teacher
;; :body (lambda ()
;; (learn-ocaml-give-token-cmd
;; (lambda (token)
;; (learn-ocaml-test-remove-client-file)
;; (learn-ocaml-init
;; :new-server-value learn-ocaml-test-url
;; :new-token-value token
;; :callback (lambda (_)
;; (learn-ocaml-give-token-cmd
;; (lambda (token2)
;; (should (equal token token2))
;; (funcall done))))))))))
;; ...

(ert-deftest-async a11_learn-ocaml-on-load-test-create-token-no-config (done)
(learn-ocaml-test-run-with
Expand All @@ -108,7 +95,7 @@
(learn-ocaml-init
:new-server-value learn-ocaml-test-url
:nickname "Foo"
:secret ""
:secret (learn-ocaml-test-secret)
:callback (lambda (_)
(learn-ocaml-give-token-cmd
(lambda (token2)
Expand Down
2 changes: 1 addition & 1 deletion tests/003-use-passwd/runtests.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
:login email
:password pass
:nickname (format "FooStudentWithEmail(%s)" email)
:secret ""
:secret (learn-ocaml-test-secret)
:callback-err
(lambda (output) (error "a12_learn-ocaml-test-sign-up: failed with [%s]." output))
:callback-ok
Expand Down
5 changes: 1 addition & 4 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,23 @@ To avoid this, you can use the dockerized version, `make tests`.

* `2_learn-ocaml-token-management-test` (**002-use-token**)
* Create token from nickname, secret (`learn-ocaml-create-token-cmd`)
* **TODO** Add secret to `server_config.json`
* Set token (`learn-ocaml-use-metadata-cmd`)
* Read token (`learn-ocaml-give-token-cmd`)
* `8_learn-ocaml-init-another-token` (**002-use-token**)
* Create token from nickname, secret (`learn-ocaml-create-token-cmd`)
* **TODO** Add secret to `server_config.json`
* Test `learn-ocaml-init` {used by `learn-ocaml-login-with-token`} with new token
* Read new token (`learn-ocaml-give-token-cmd`)
* They should be equal
* `9_learn-ocaml-init-create-token` (**002-use-token**)
* Read token (`learn-ocaml-give-token-cmd`)
* Test `learn-ocaml-init` {used by `learn-ocaml-login-with-token`} with no token
* Test `learn-ocaml-init` {used by `learn-ocaml-login-with-token`} with no token, secret
* which will call `learn-ocaml-create-token-cmd`
* Read new token (`learn-ocaml-give-token-cmd`)
* It should be different
* `a11_learn-ocaml-on-load-test-create-token-no-config` (**002-use-token**)
* Read token (`learn-ocaml-give-token-cmd`)
* `rm -f ~/.config/learnocaml/client.json`
* Test `learn-ocaml-init` {used by `learn-ocaml-login-with-token`} with URL, nickname, secret
* **TODO** Add secret to `server_config.json`
* Read token (`learn-ocaml-give-token-cmd`)
* **FIXME** It should be different

Expand Down
10 changes: 7 additions & 3 deletions tests/learn-ocaml-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ See also `learn-ocaml-client-sign-up-cmd'.")
(format "test%d-%d@example.com" learn-ocaml-test-user-num rand)))

(defun learn-ocaml-test-user-pass ()
"Return a dummy password for creating test accounts."
"OCaml123_")

(defun learn-ocaml-test-secret ()
"Return the learn-ocaml secret for creating accounts."
"ServerPass")

;;; NOTE: This symbol list gather tests specific to 'use_passwd: true'
;;; (setq learn-ocaml-test-use-passwd-list
;;; '(a12_learn-ocaml-test-sign-up 2_learn-ocaml-token-management-test))
Expand Down Expand Up @@ -239,7 +244,6 @@ The caller must run (learn-ocaml-test-remove-client-file) manually afterwards."
:server learn-ocaml-test-url
:token (learn-ocaml-test-get-teacher-token)
:nickname "Teacher"
:secret ""
:callback (lambda (_) (funcall body))))
;; Note: this form completes immediately *but* the async test runs in the bg.

Expand All @@ -248,7 +252,7 @@ The caller must run (learn-ocaml-test-remove-client-file) manually afterwards."
(learn-ocaml-init-cmd
:server learn-ocaml-test-url
:nickname "Student"
:secret ""
:secret (learn-ocaml-test-secret)
:callback (lambda (_) (funcall body)))
(let ((email (learn-ocaml-test-user-email))
(pass (learn-ocaml-test-user-pass)))
Expand All @@ -257,7 +261,7 @@ The caller must run (learn-ocaml-test-remove-client-file) manually afterwards."
:login email
:password pass
:nickname (format "StudentWithEmail(%s)" email)
:secret ""
:secret (learn-ocaml-test-secret)
:callback-err
(lambda (output) (error "learn-ocaml-test-run-with: learn-ocaml-client-sign-up-cmd: failed with [%s]." output))
:callback-ok
Expand Down
1 change: 1 addition & 0 deletions tests/use_passwd.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"secret": "ServerPass",
"use_passwd": true
}
3 changes: 3 additions & 0 deletions tests/use_token.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"secret": "ServerPass"
}

0 comments on commit 3646c72

Please sign in to comment.