|
| 1 | +## This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +## License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +## file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +## |
| 5 | +## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. |
| 6 | + |
| 7 | +defmodule HashPasswordCommandTest do |
| 8 | + use ExUnit.Case, async: false |
| 9 | + import TestHelper |
| 10 | + |
| 11 | + @command RabbitMQ.CLI.Ctl.Commands.HashPasswordCommand |
| 12 | + |
| 13 | + setup_all do |
| 14 | + RabbitMQ.CLI.Core.Distribution.start() |
| 15 | + :ok |
| 16 | + end |
| 17 | + |
| 18 | + setup context do |
| 19 | + on_exit(context, fn -> delete_user(context[:user]) end) |
| 20 | + {:ok, opts: %{node: get_rabbit_hostname()}} |
| 21 | + end |
| 22 | + |
| 23 | + test "validate: too many arguments", context do |
| 24 | + assert @command.validate(["foo", "bar"], context[:opts]) == |
| 25 | + {:validation_failure, :too_many_args} |
| 26 | + end |
| 27 | + |
| 28 | + test "validate: too few arguments", context do |
| 29 | + assert @command.validate([], context[:opts]) == {:validation_failure, :not_enough_args} |
| 30 | + end |
| 31 | + |
| 32 | + test "validate: empty string", context do |
| 33 | + assert @command.validate([""], context[:opts]) == |
| 34 | + {:bad_argument, "password cannot be an empty string"} |
| 35 | + end |
| 36 | + |
| 37 | + @tag user: "someone", password: "hashed_password" |
| 38 | + test "run: successfully create user with a hashed password from cli cmd", context do |
| 39 | + hashed_pwd = @command.run([context[:password]], context[:opts]) |
| 40 | + add_user_hashed_password(context[:user], hashed_pwd) |
| 41 | + assert {:ok, _} = authenticate_user(context[:user], context[:password]) |
| 42 | + end |
| 43 | + |
| 44 | + @tag user: "someone", password: "hashed_password" |
| 45 | + test "run: Create user with a hashed password from cli cmd, use hashed pwd as cleartest password", |
| 46 | + context do |
| 47 | + hashed_pwd = @command.run([context[:password]], context[:opts]) |
| 48 | + add_user_hashed_password(context[:user], hashed_pwd) |
| 49 | + assert {:refused, _, _, _} = authenticate_user(context[:user], hashed_pwd) |
| 50 | + end |
| 51 | +end |
0 commit comments