From 21bdc91b248dd1db3141621ecaacbbb42a44776b Mon Sep 17 00:00:00 2001 From: Milton Mazzarri Date: Tue, 19 Mar 2019 23:55:29 -0500 Subject: [PATCH] Add unit tests for saving/loading credentials --- lib/hunter/application.ex | 2 +- lib/hunter/config.ex | 3 +- test/hunter/application_test.exs | 53 +++++++++++++++++++++++++++++--- test/test_helper.exs | 6 ++++ 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/hunter/application.ex b/lib/hunter/application.ex index 56fadd2..27d5ed6 100644 --- a/lib/hunter/application.ex +++ b/lib/hunter/application.ex @@ -87,7 +87,7 @@ defmodule Hunter.Application do * `name` - application name """ - @spec load_credentials(String.t()) :: Hunter.Application.t() + @spec load_credentials(String.t()) :: Hunter.Application.t() | no_return def load_credentials(name) do Hunter.Config.home() |> Path.join("apps/#{name}.json") diff --git a/lib/hunter/config.ex b/lib/hunter/config.ex index 751ba7c..2ba992f 100644 --- a/lib/hunter/config.ex +++ b/lib/hunter/config.ex @@ -39,7 +39,8 @@ defmodule Hunter.Config do """ def home do - Path.expand(System.get_env("HUNTER_HOME") || "~/.hunter") + home = System.get_env("HUNTER_HOME") || Application.get_env(:hunter, :home, "~/.hunter") + Path.expand(home) end @doc """ diff --git a/test/hunter/application_test.exs b/test/hunter/application_test.exs index 5ff06e1..880f025 100644 --- a/test/hunter/application_test.exs +++ b/test/hunter/application_test.exs @@ -3,17 +3,15 @@ defmodule Hunter.ApplicationTest do import Mox - alias Hunter.Application - setup :verify_on_exit! test "should allow to create an app" do expect(Hunter.ApiMock, :create_app, fn _client, _redirect, _scopes, _website, _opts -> - %Application{client_id: "1234567890", client_secret: "1234567890", id: 1234} + %Hunter.Application{client_id: "1234567890", client_secret: "1234567890", id: 1234} end) - assert %Application{client_id: "1234567890", client_secret: "1234567890", id: 1234} == - Application.create_app( + assert %Hunter.Application{client_id: "1234567890", client_secret: "1234567890", id: 1234} == + Hunter.Application.create_app( "hunter", "urn:ietf:wg:oauth:2.0:oob", ["read", "write", "follow"], @@ -22,4 +20,49 @@ defmodule Hunter.ApplicationTest do api_base_url: "https://example.com" ) end + + test "should allow to store credentials on home directory" do + expect(Hunter.ApiMock, :create_app, fn _client, _redirect, _scopes, _website, _opts -> + %Hunter.Application{client_id: "1234567890", client_secret: "1234567890", id: 1234} + end) + + home = Hunter.Config.home() + tmp_dir = Path.expand("../../tmp", __DIR__) + Application.put_env(:hunter, :home, tmp_dir) + app_name = "hunter" + + assert %Hunter.Application{client_id: "1234567890", client_secret: "1234567890", id: 1234} = + result = + Hunter.Application.create_app( + app_name, + "urn:ietf:wg:oauth:2.0:oob", + ["read"], + nil, + save?: true, + api_base_url: "https://example.com" + ) + + assert result == Hunter.Application.load_credentials(app_name) + Application.put_env(:hunter, :home, home) + end + + test "should allow to load persisted app's credentials" do + home = Hunter.Config.home() + tmp_dir = Path.expand("../../tmp", __DIR__) + app_dir = Path.join(tmp_dir, "apps") + app_name = "load" + Application.put_env(:hunter, :home, tmp_dir) + + expected = %{id: 1234, client_secret: "1234567890", client_id: "1234567890"} + + unless File.exists?(app_dir), do: File.mkdir_p!(app_dir) + + File.write!("#{app_dir}/#{app_name}.json", Poison.encode!(expected)) + + assert %Hunter.Application{} = app = Hunter.Application.load_credentials(app_name) + + assert Map.equal?(Map.from_struct(app), expected) + + Application.put_env(:hunter, :home, home) + end end diff --git a/test/test_helper.exs b/test/test_helper.exs index d695673..ebbfcd5 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,3 +1,9 @@ ExUnit.start() Mox.defmock(Hunter.ApiMock, for: Hunter.Api) Application.put_env(:hunter, :hunter_api, Hunter.ApiMock) + +ExUnit.after_suite(fn _ -> + "../tmp" + |> Path.expand(__DIR__) + |> File.rm_rf() +end)