From cd9f648f6a68c893922b01d261641826a50984a5 Mon Sep 17 00:00:00 2001 From: Yasunari Watanabe Date: Wed, 23 Dec 2020 15:43:00 +0800 Subject: [PATCH] add tests --- test/dune | 16 +++++++++-- test/slack_oauth.expected | 13 +++++++++ test/slack_oauth_test.ml | 58 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 test/slack_oauth.expected create mode 100644 test/slack_oauth_test.ml diff --git a/test/dune b/test/dune index 272bc3e0..0d2a7525 100644 --- a/test/dune +++ b/test/dune @@ -1,5 +1,5 @@ -(executable - (name test) +(executables + (names test slack_oauth_test) (libraries lib base base.caml devkit devkit.core extlib lwt.unix stdio yojson) (preprocess @@ -21,3 +21,15 @@ (alias runtest) (action (diff slack_payloads.expected slack_payloads.out))) + +(rule + (deps notabot.json secrets.json) + (action + (with-stdout-to + slack_oauth.out + (run ./slack_oauth_test.exe)))) + +(rule + (alias runtest) + (action + (diff slack_oauth.expected slack_oauth.out))) diff --git a/test/slack_oauth.expected b/test/slack_oauth.expected new file mode 100644 index 00000000..069e406f --- /dev/null +++ b/test/slack_oauth.expected @@ -0,0 +1,13 @@ +===== slack oauth: success, valid params ==== +will generate token +===== slack oauth: success, no signing key ==== +will generate token +===== slack oauth: success, with state ==== +will generate token +===== slack oauth: no-op, token exists ==== +===== slack oauth: failure, no state arg ==== +===== slack oauth: failure, bad state arg ==== +===== slack oauth: failure, no code arg ==== +===== slack oauth: failure, bad timestamp ==== +===== slack oauth: failure, no signature ==== +===== slack oauth: failure, no timestamp ==== diff --git a/test/slack_oauth_test.ml b/test/slack_oauth_test.ml new file mode 100644 index 00000000..2e7c2e9a --- /dev/null +++ b/test/slack_oauth_test.ml @@ -0,0 +1,58 @@ +open Base +open Lib +module Action = Action.Action (Api_local.Github) (Api_local.Slack) + +let signing_key = "8f742231b10e8888abcd99yyyzzz85a5" + +let sig_header = "x-slack-signature", "v0=abb033014f155dcde840b8dc150b1fb12433b8f8233ffc2853be34e151bad9e8" + +let ts_valid = "x-slack-request-timestamp", "1531420618" + +let ts_invalid = "x-slack-request-timestamp", "5000000000" + +let arg_code = "code", "1591663521684.1613458437648.4a18cf683e541ff9d8fd75075181cac49c7acae9431d7e4ffd424ce1ca8d2543" + +let arg_state = "state", "foo" + +let arg_state_bad = "state", "bar" + +let secrets_default : Config_t.secrets = + { + gh_token = None; + gh_hook_token = None; + slack_client_id = ""; + slack_client_secret = ""; + slack_oauth_state = None; + slack_signing_secret = None; + slack_access_token = None; + } + +let secrets_with_key = { secrets_default with slack_signing_secret = Some signing_key } + +let secrets_with_state = { secrets_default with slack_oauth_state = Some "foo" } + +let secrets_with_token = { secrets_default with slack_access_token = Some "123" } + +(* name, args, headers, body *) +let tests = + [ + "success, valid params", [ arg_code ], [ sig_header; ts_valid ], "text=hello", secrets_with_key; + "success, no signing key", [ arg_code ], [ sig_header; ts_valid ], "text=hello", secrets_default; + "success, with state", [ arg_code; arg_state ], [ sig_header; ts_valid ], "text=hello", secrets_with_state; + "no-op, token exists", [ arg_code ], [ sig_header; ts_valid ], "text=hello", secrets_with_token; + "failure, no state arg", [ arg_code ], [ sig_header; ts_valid ], "text=hello", secrets_with_state; + "failure, bad state arg", [ arg_code; arg_state_bad ], [ sig_header; ts_valid ], "text=hello", secrets_with_state; + "failure, no code arg", [], [ sig_header; ts_valid ], "text=hello", secrets_with_key; + "failure, bad timestamp", [ arg_code ], [ sig_header; ts_invalid ], "text=hello", secrets_with_key; + "failure, no signature", [ arg_code ], [ ts_valid ], "text=hello", secrets_with_key; + "failure, no timestamp", [ arg_code ], [ sig_header ], "text=hello", secrets_with_key; + ] + +let process (ctx : Context.t) (name, args, headers, body, secrets) = + ctx.secrets <- Some secrets; + Stdio.printf "===== slack oauth: %s ====\n" name; + Action.process_slack_oauth ctx args headers body + +let () = + let ctx = Context.make () in + Lwt_main.run @@ Lwt_list.iter_s (process ctx) tests