-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #305
- Loading branch information
Showing
4 changed files
with
209 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,53 @@ | ||
defmodule Mix.Tasks.Hex.Key do | ||
use Mix.Task | ||
alias Mix.Hex.Utils | ||
|
||
@shortdoc "Manages Hex API key" | ||
|
||
@moduledoc """ | ||
Removes or lists API keys associated with your account. | ||
### Remove key | ||
Removes given API key from account. | ||
The key can no longer be used to authenticate API requests. | ||
mix hex.key remove key_name | ||
To remove all API keys from your account, pass the `--all` option. | ||
mix hex.key remove --all | ||
### List keys | ||
Lists all API keys associated with your account. | ||
mix hex.key list | ||
""" | ||
@moduledoc false | ||
|
||
@switches [all: :boolean] | ||
|
||
def run(args) do | ||
Hex.start | ||
{opts, args, _} = OptionParser.parse(args, switches: @switches) | ||
config = Hex.Config.read | ||
all? = Keyword.get(opts, :all, false) | ||
|
||
case args do | ||
["remove", key] -> | ||
auth = Utils.auth_info(config) | ||
remove_key(key, auth) | ||
["remove", _] -> | ||
remove_key() | ||
["remove"] when all? -> | ||
auth = Utils.auth_info(config) | ||
remove_all_keys(auth) | ||
remove_all_keys() | ||
["list"] -> | ||
auth = Utils.auth_info(config) | ||
list_keys(auth) | ||
list_keys() | ||
_ -> | ||
Mix.raise """ | ||
Invalid arguments, expected one of: | ||
mix hex.key remove KEY | ||
mix hex.key remove --all | ||
mix hex.key list | ||
mix hex.user --remove KEY | ||
mix hex.user --remove-all | ||
mix hex.user --list-keys | ||
""" | ||
end | ||
end | ||
|
||
defp remove_key(key, auth) do | ||
Hex.Shell.info "Removing key #{key}..." | ||
case Hex.API.Key.delete(key, auth) do | ||
{200, %{"name" => ^key, "authing_key" => true}, _headers} -> | ||
Mix.Tasks.Hex.User.run(["deauth"]) | ||
:ok | ||
{code, _body, _headers} when code in 200..299 -> | ||
:ok | ||
{code, body, _headers} -> | ||
Hex.Shell.error "Key removal failed" | ||
Hex.Utils.print_error_result(code, body) | ||
end | ||
defp remove_key() do | ||
deprecation_msg = """ | ||
[deprecation] Calling mix hex.key remove KEY is deprecated, please use: | ||
mix hex.user --remove KEY | ||
""" | ||
Mix.raise deprecation_msg | ||
end | ||
|
||
defp remove_all_keys(auth) do | ||
Hex.Shell.info "Removing all keys..." | ||
case Hex.API.Key.delete_all(auth) do | ||
{code, %{"name" => _, "authing_key" => true}, _headers} when code in 200..299 -> | ||
Mix.Tasks.Hex.User.run(["deauth"]) | ||
:ok | ||
{code, body, _headers} -> | ||
Hex.Shell.error "Key removal failed" | ||
Hex.Utils.print_error_result(code, body) | ||
end | ||
defp remove_all_keys() do | ||
deprecation_msg = """ | ||
[deprecation] Calling mix hex.key remove --all is deprecated, please use: | ||
mix hex.user --remove-all | ||
""" | ||
Mix.raise deprecation_msg | ||
end | ||
|
||
defp list_keys(auth) do | ||
case Hex.API.Key.get(auth) do | ||
{code, body, _headers} when code in 200..299 -> | ||
values = Enum.map(body, fn %{"name" => name, "inserted_at" => time} -> | ||
[name, time] | ||
end) | ||
Utils.print_table(["Name", "Created at"], values) | ||
{code, body, _headers} -> | ||
Hex.Shell.error "Key fetching failed" | ||
Hex.Utils.print_error_result(code, body) | ||
end | ||
defp list_keys() do | ||
deprecation_msg = """ | ||
[deprecation] Calling mix hex.key list is deprecated, please use: | ||
mix hex.user --list-keys | ||
""" | ||
Mix.raise deprecation_msg | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,36 @@ | ||
defmodule Mix.Tasks.Hex.KeyTest do | ||
use HexTest.Case | ||
@moduletag :integration | ||
|
||
test "list keys" do | ||
in_tmp fn -> | ||
Hex.State.put(:home, System.cwd!) | ||
deprecation_msg = """ | ||
[deprecation] Calling mix hex.key list is deprecated, please use: | ||
mix hex.user --list-keys | ||
""" | ||
|
||
auth = HexWeb.new_user("list_keys", "[email protected]", "password", "list_keys") | ||
Hex.Config.update(auth) | ||
|
||
assert {200, [%{"name" => "list_keys"}], _} = Hex.API.Key.get(auth) | ||
|
||
send self(), {:mix_shell_input, :prompt, "password"} | ||
assert_raise Mix.Error, deprecation_msg, fn -> | ||
Mix.Tasks.Hex.Key.run(["list"]) | ||
assert_received {:mix_shell, :info, ["list_keys" <> _]} | ||
end | ||
end | ||
|
||
test "remove key" do | ||
in_tmp fn -> | ||
Hex.State.put(:home, System.cwd!) | ||
|
||
auth_a = HexWeb.new_user("remove_key", "[email protected]", "password", "remove_key_a") | ||
auth_b = HexWeb.new_key("remove_key", "password", "remove_key_b") | ||
Hex.Config.update(auth_a) | ||
|
||
assert {200, _, _} = Hex.API.Key.get(auth_a) | ||
assert {200, _, _} = Hex.API.Key.get(auth_b) | ||
|
||
send self(), {:mix_shell_input, :prompt, "password"} | ||
Mix.Tasks.Hex.Key.run(["remove", "remove_key_b"]) | ||
assert_received {:mix_shell, :info, ["Removing key remove_key_b..."]} | ||
|
||
assert {200, _, _} = Hex.API.Key.get(auth_a) | ||
assert {401, _, _} = Hex.API.Key.get(auth_b) | ||
deprecation_msg = """ | ||
[deprecation] Calling mix hex.key remove KEY is deprecated, please use: | ||
mix hex.user --remove KEY | ||
""" | ||
|
||
send self(), {:mix_shell_input, :prompt, "password"} | ||
Mix.Tasks.Hex.Key.run(["remove", "remove_key_a"]) | ||
assert_received {:mix_shell, :info, ["Removing key remove_key_a..."]} | ||
assert_received {:mix_shell, :info, ["User `remove_key` removed from the local machine. To authenticate again, run `mix hex.user auth` or create a new user with `mix hex.user register`"]} | ||
|
||
assert {401, _, _} = Hex.API.Key.get(auth_a) | ||
|
||
config = Hex.Config.read | ||
refute config[:username] | ||
refute config[:key] | ||
refute config[:encrypted_key] | ||
assert_raise Mix.Error, deprecation_msg, fn -> | ||
Mix.Tasks.Hex.Key.run(["remove", "key"]) | ||
end | ||
end | ||
|
||
test "remove all keys" do | ||
in_tmp fn -> | ||
Hex.State.put(:home, System.cwd!) | ||
|
||
auth_a = HexWeb.new_user("remove_all_keys", "[email protected]", "password", "remove_all_keys_a") | ||
auth_b = HexWeb.new_key("remove_all_keys", "password", "remove_all_keys_b") | ||
Hex.Config.update(auth_a) | ||
deprecation_msg = """ | ||
[deprecation] Calling mix hex.key remove --all is deprecated, please use: | ||
mix hex.user --remove-all | ||
""" | ||
|
||
assert {200, _, _} = Hex.API.Key.get(auth_a) | ||
assert {200, _, _} = Hex.API.Key.get(auth_b) | ||
|
||
send self(), {:mix_shell_input, :prompt, "password"} | ||
assert_raise Mix.Error, deprecation_msg, fn -> | ||
Mix.Tasks.Hex.Key.run(["remove", "--all"]) | ||
assert_received {:mix_shell, :info, ["Removing all keys..."]} | ||
assert_received {:mix_shell, :info, ["User `remove_all_keys` removed from the local machine. To authenticate again, run `mix hex.user auth` or create a new user with `mix hex.user register`"]} | ||
|
||
assert {401, _, _} = Hex.API.Key.get(auth_a) | ||
assert {401, _, _} = Hex.API.Key.get(auth_b) | ||
|
||
config = Hex.Config.read | ||
refute config[:username] | ||
refute config[:key] | ||
refute config[:encrypted_key] | ||
end | ||
end | ||
end |
Oops, something went wrong.