Skip to content

Commit

Permalink
Create base for add_alias tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl-Aksel Puulmann committed Feb 10, 2017
1 parent 3add0f6 commit 1395915
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
35 changes: 18 additions & 17 deletions lib/pagerbot/plugin/add_alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(config)
def self.manual
{
description: "Add alias for user or schedule",
syntax: ["alias FIELDVALUE as NEW_ALIAS"],
syntax: ["alias USER_OR_SCHEDULE as NEW_ALIAS"],
examples: [
"alias karl@mycompany.com as thebestkarl",
"alias PIBRSYV as triage"
Expand All @@ -21,49 +21,50 @@ def self.manual
def parse(query)
return unless query[:command] == "alias"

parse_stage = :field_value
result = {
field_value: [],
new_alias: [],
parse_stage = :searched_alias
names = {
searched_alias: [],
new_alias: []
}

query[:words].each do |word|
case word
when 'as'
parse_stage = :new_alias
else
result[parse_stage] << word
names[parse_stage] << word
end
end

join_all(result)
names.each do |stage, nameparts|
names[stage] = nameparts.join(' ')
end
end

def matches_in(collection, expected)
def matches_in(collection, nameCandidate)
fields_to_scan = [:email, :name, :id]

normalized = PagerBot::Utilities.normalize(expected)
normalizedCandidate = PagerBot::Utilities.normalize(nameCandidate)

collection.list.select do |member|
collection.select do |member|
fields_to_scan.any? do |field|
member[field] && member.normalized(field) == normalized
member[field] && PagerBot::Utilities.normalize(member[field]) == normalizedCandidate
end
end
end

+PagerBot::Utilities::DispatchMethod
def dispatch(query, event_data)
users_matches = matches_in(pagerduty.users, query[:field_value])
schedule_matches = matches_in(pagerduty.schedules, query[:field_value])
users_matches = matches_in(pagerduty.users.list, query[:searched_alias])
schedule_matches = matches_in(pagerduty.schedules.list, query[:searched_alias])
new_alias = PagerBot::Utilities.normalize(query[:new_alias])

# if no matches
if users_matches.empty? && schedule_matches.empty?
return "Could not find #{query[:field_value]}."
return "Could not find user or schedule #{query[:searched_alias]}."
end
# if too many matches
# Ambigious matches.
if users_matches.length + schedule_matches.length > 1
return "Field #{query[:field_value]} is ambiguous."
return "Field #{query[:searched_alias]} is ambiguous."
end
# base case, only one find!
store = PagerBot::DataStore.new
Expand Down
8 changes: 0 additions & 8 deletions lib/pagerbot/plugin/plugin_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ def get_plugin(name)
plugin
end

def join_all(hash)
hash.each do |key, value|
if value.is_a? Array
hash[key] = value.join(' ')
end
end
end

def log
PagerBot.log
end
Expand Down
34 changes: 34 additions & 0 deletions test/unit/pagerbot/plugins/add_alias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative('../../../_lib')

class AddAlias < Critic::MockedPagerDutyTest
before do
@plugin = PagerBot::PluginManager.load_plugin "add_alias", {}
end

describe 'Adding an alias to user or schedule' do
describe 'parsing' do
it 'should parse a simple example' do
got = @plugin.parse({
command: "alias",
words: "karl@mycompany.com as the best-karl".split
})
assert_equal got, {
searched_alias: 'karl@mycompany.com',
new_alias: 'the best-karl'
}
end
end

describe 'matching' do
it 'should match on id, name, email by default if they are present' do
assert_equal @plugin.matches_in([name: 'foo'], 'foo'), [name: 'foo']
assert_equal @plugin.matches_in([name: 'foobar'], 'foo'), []
assert_equal @plugin.matches_in([], 'aaa'), []
assert_equal @plugin.matches_in([{}], 'aaa'), []
assert_equal @plugin.matches_in([name: nil], 'aaa'), []
assert_equal @plugin.matches_in([email: 'foo'], 'foo'), [email: 'foo']
assert_equal @plugin.matches_in([id: 'foo'], 'foo'), [id: 'foo']
end
end
end
end

0 comments on commit 1395915

Please sign in to comment.