Add unit tests for saving/loading credentials
parent
0d1f047477
commit
21bdc91b24
|
@ -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")
|
||||
|
|
|
@ -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 """
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue