1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
defmodule SetUserTagsCommandTest do
use ExUnit.Case, async: false
import TestHelper
@command RabbitMQ.CLI.Ctl.Commands.SetUserTagsCommand
@user "user1"
@password "password"
setup_all do
RabbitMQ.CLI.Core.Distribution.start()
add_user @user, @password
on_exit([], fn ->
delete_user(@user)
end)
:ok
end
setup context do
context[:user] # silences warnings
on_exit([], fn -> set_user_tags(context[:user], []) end)
{:ok, opts: %{node: get_rabbit_hostname()}}
end
test "validate: on an incorrect number of arguments, return an arg count error" do
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args}
end
test "run: throws a badrpc when instructed to contact an unreachable RabbitMQ node" do
opts = %{node: :jake@thedog, timeout: 200}
assert match?({:badrpc, _}, @command.run([@user, :imperator], opts))
end
@tag user: @user, tags: [:imperator]
test "run: on a single optional argument, add a flag to the user", context do
@command.run(
[context[:user] | context[:tags]],
context[:opts]
)
result = Enum.find(
list_users(),
fn(record) -> record[:user] == context[:user] end
)
assert result[:tags] == context[:tags]
end
@tag user: "interloper", tags: [:imperator]
test "run: on an invalid user, get a no such user error", context do
assert @command.run(
[context[:user] | context[:tags]],
context[:opts]
) == {:error, {:no_such_user, context[:user]}}
end
@tag user: @user, tags: [:imperator, :generalissimo]
test "run: on multiple optional arguments, add all flags to the user", context do
@command.run(
[context[:user] | context[:tags]],
context[:opts]
)
result = Enum.find(
list_users(),
fn(record) -> record[:user] == context[:user] end
)
assert result[:tags] == context[:tags]
end
@tag user: @user, tags: [:imperator]
test "run: with no optional arguments, clear user tags", context do
set_user_tags(context[:user], context[:tags])
@command.run([context[:user]], context[:opts])
result = Enum.find(
list_users(),
fn(record) -> record[:user] == context[:user] end
)
assert result[:tags] == []
end
@tag user: @user, tags: [:imperator]
test "run: identical calls are idempotent", context do
set_user_tags(context[:user], context[:tags])
assert @command.run(
[context[:user] | context[:tags]],
context[:opts]
) == :ok
result = Enum.find(
list_users(),
fn(record) -> record[:user] == context[:user] end
)
assert result[:tags] == context[:tags]
end
@tag user: @user, old_tags: [:imperator], new_tags: [:generalissimo]
test "run: if different tags exist, overwrite them", context do
set_user_tags(context[:user], context[:old_tags])
assert @command.run(
[context[:user] | context[:new_tags]],
context[:opts]
) == :ok
result = Enum.find(
list_users(),
fn(record) -> record[:user] == context[:user] end
)
assert result[:tags] == context[:new_tags]
end
@tag user: @user, tags: ["imperator"]
test "banner", context do
assert @command.banner(
[context[:user] | context[:tags]],
context[:opts]
)
=~ ~r/Setting tags for user \"#{context[:user]}\" to \[#{context[:tags]}\] \.\.\./
end
end
|