From 84c5263e8c6b6523d5c30a13e1b0252e62d0b86f Mon Sep 17 00:00:00 2001 From: Tom Haines Date: Wed, 26 Aug 2020 15:01:04 +0100 Subject: [PATCH] #21: Add testing for new func & task --- test/mix/tasks/smart_home_test.exs | 17 +++++++++++++++++ test/smart_home_auth/helper_test.exs | 11 +++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/mix/tasks/smart_home_test.exs create mode 100644 test/smart_home_auth/helper_test.exs diff --git a/test/mix/tasks/smart_home_test.exs b/test/mix/tasks/smart_home_test.exs new file mode 100644 index 0000000..b4e42c4 --- /dev/null +++ b/test/mix/tasks/smart_home_test.exs @@ -0,0 +1,17 @@ +defmodule Mix.Tasks.SmartHomeTest do + use ExUnit.Case + import ExUnit.CaptureIO + + require Logger + + @jwt_pattern ~r/[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+\/=]*$/m + + test "Mix task returns a valid JWT" do + out = capture_io(fn -> Mix.Tasks.SmartHome.GenToken.run([]) end) + + jwt = Regex.run(@jwt_pattern, out) |> List.first() + + decoded = AuthPlug.Token.verify_jwt!(jwt) + assert %{"id" => 1} = decoded + end +end diff --git a/test/smart_home_auth/helper_test.exs b/test/smart_home_auth/helper_test.exs new file mode 100644 index 0000000..283b3bd --- /dev/null +++ b/test/smart_home_auth/helper_test.exs @@ -0,0 +1,11 @@ +defmodule SmartHomeAuth.HelperTest do + use ExUnit.Case, async: true + + test "gen_auth_token generates a correct token" do + token = SmartHomeAuth.gen_auth_token() + + decoded = AuthPlug.Token.verify_jwt!(token) + + assert %{"id" => 1} = decoded + end +end