Module: Chatops::Controller::TestCaseHelpers

Included in:
TestCase
Defined in:
lib/chatops/controller/test_case_helpers.rb

Defined Under Namespace

Classes: NoMatchingCommandRegex

Instance Method Summary collapse

Instance Method Details

#chat(message, user, room_id = "123") ⇒ Object



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
# File 'lib/chatops/controller/test_case_helpers.rb', line 38

def chat(message, user, room_id = "123")
  get :list
  json_response = JSON.load(response.body)
  matchers = json_response["methods"].map { |name, |
     = .dup
    ["name"] = name
    prefix = chatops_prefix ? "#{chatops_prefix} " : ""
    ["regex"] = Regexp.new("^#{prefix}#{["regex"]}$", "i")
    
  }

  named_params, command = extract_named_params(message)

  matcher = matchers.find { |matcher| matcher["regex"].match(command) }

  raise NoMatchingCommandRegex.new("No command matches '#{command}'") unless matcher

  match_data = matcher["regex"].match(command)
  jsonrpc_params = named_params.dup
  matcher["params"].each do |param|
    jsonrpc_params[param] = match_data[param.to_sym]
  end
  jsonrpc_params.merge!(user: user, room_id: room_id)
  chatop matcher["name"].to_sym, jsonrpc_params
end

#chatop(method, params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chatops/controller/test_case_helpers.rb', line 19

def chatop(method, params = {})
  args = params.dup.symbolize_keys
  user = args.delete :user
  room_id = args.delete :room_id

  params = {
    :params => args,
    :room_id => room_id,
    :user => user
  }

  major_version = Rails.version.split('.')[0].to_i
  if major_version >= 5
    post :execute_chatop, params: params.merge(chatop: method)
  else
    post :execute_chatop, params.merge(chatop: method)
  end
end

#chatop_errorObject



72
73
74
75
# File 'lib/chatops/controller/test_case_helpers.rb', line 72

def chatop_error
  json_response = JSON.load(response.body)
  json_response["error"]["message"]
end

#chatop_responseObject



64
65
66
67
68
69
70
# File 'lib/chatops/controller/test_case_helpers.rb', line 64

def chatop_response
  json_response = JSON.load(response.body)
  if json_response["error"].present?
    raise "There was an error instead of an expected successful response: #{json_response["error"]}"
  end
  json_response["result"]
end

#chatops_auth!Object



5
6
7
# File 'lib/chatops/controller/test_case_helpers.rb', line 5

def chatops_auth!
  request.env["CHATOPS_TESTING_AUTH"] = true
end

#chatops_prefix(prefix = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/chatops/controller/test_case_helpers.rb', line 9

def chatops_prefix(prefix = nil)
  # We abuse request.env here so that rails will cycle this with each test.
  # If we used an instance variable, one would always need to be resetting
  # it.
  if prefix
    request.env["CHATOPS_TESTING_PREFIX"] = prefix
  end
  request.env["CHATOPS_TESTING_PREFIX"]
end

#extract_named_params(command_string) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chatops/controller/test_case_helpers.rb', line 77

def extract_named_params(command_string)
  params = {}

  while last_index = command_string.rindex(" --")
    arg = command_string[last_index..-1]
    matches = arg.match(/ --(\S+)(.*)/)
    params[matches[1]] = matches[2].strip
    params[matches[1]] = "true" unless params[matches[1]].present?
    command_string = command_string.slice(0, last_index)
  end

  command_string = command_string.strip
  [params, command_string]
end