Class: Foodtaster::RSpec::Matchers::UserMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/foodtaster/rspec/matchers/user_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(username) ⇒ UserMatcher

Returns a new instance of UserMatcher.



5
6
7
# File 'lib/foodtaster/rspec/matchers/user_matcher.rb', line 5

def initialize(username)
  @username = username
end

Instance Method Details

#descriptionObject



50
51
52
53
# File 'lib/foodtaster/rspec/matchers/user_matcher.rb', line 50

def description
  ["have user '#{@username}'",
    @group && "in group #{@group}"].delete_if { |a| !a }.join(" ")
end

#failure_message_for_shouldObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/foodtaster/rspec/matchers/user_matcher.rb', line 32

def failure_message_for_should
  msg = ["expected that #{@vm.name} should have user '#{@username}'"]

  if @group
    msg << "in group #{@group.inspect}"

    if @results.key?(:group) && !@results[:group]
      msg << " but actual user groups are:\n#{@actual_groups.join(", ")}\n"
    end
  end

  msg.join(" ")
end

#failure_message_for_should_notObject



46
47
48
# File 'lib/foodtaster/rspec/matchers/user_matcher.rb', line 46

def failure_message_for_should_not
  "expected that #{@vm.name} should not have user '#{@username}'"
end

#in_group(group) ⇒ Object



26
27
28
29
30
# File 'lib/foodtaster/rspec/matchers/user_matcher.rb', line 26

def in_group(group)
  @group = group

  self
end

#matches?(vm) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/foodtaster/rspec/matchers/user_matcher.rb', line 9

def matches?(vm)
  @vm = vm
  @results = {}

  unless vm.execute("cat /etc/passwd | cut -d: -f1 | grep \"\\<#{@username}\\>\"").successful?
    @results[:user] = false
    return false
  end

  if @group
    @actual_groups = vm.execute("groups #{@username}").stdout.to_s.chomp.split(" ")[2..-1] || []
    @results[:group] = !!@actual_groups.include?(@group)
  end

  @results.values.all?
end