Skip to content

Commit dc47eb1

Browse files
Patrick BNick Woodward
Patrick B
authored and
Nick Woodward
committed
feat: allow users with space in username (#46)
* Allow for spaces in name * Add tests that account for names with spaces Closes #44
1 parent 6447ea8 commit dc47eb1

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

src/auth.coffee

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ module.exports = (robot) ->
6666
robot.auth = new Auth
6767

6868

69-
robot.respond /@?([^\s]+) ha(?:s|ve) (["'\w: -_]+) role/i, (msg) ->
69+
robot.respond /@?(.+) ha(?:s|ve) (["'\w: -_]+) role/i, (msg) ->
7070
name = msg.match[1].trim()
7171
if name.toLowerCase() is 'i' then name = msg.message.user.name
72+
if name.match(/(.*)(?:don['’]t|doesn['’]t|do not|does not)/i) then return
7273

7374
unless name.toLowerCase() in ['', 'who', 'what', 'where', 'when', 'why']
7475
unless robot.auth.isAdmin msg.message.user
@@ -90,7 +91,7 @@ module.exports = (robot) ->
9091
user.roles.push(newRole)
9192
msg.reply "OK, #{name} has the '#{newRole}' role."
9293

93-
robot.respond /@?([^\s]+) (?:don['’]t|doesn['’]t|do not) have (["'\w: -_]+) role/i, (msg) ->
94+
robot.respond /@?(.+) (?:don['’]t|doesn['’]t|do not|does not) have (["'\w: -_]+) role/i, (msg) ->
9495
name = msg.match[1].trim()
9596
if name.toLowerCase() is 'i' then name = msg.message.user.name
9697

@@ -111,7 +112,7 @@ module.exports = (robot) ->
111112
user.roles = (role for role in user.roles when role isnt newRole)
112113
msg.reply "OK, #{name} doesn't have the '#{newRole}' role."
113114

114-
robot.respond /what roles? do(es)? @?([^\s]+) have\?*$/i, (msg) ->
115+
robot.respond /what roles? do(es)? @?(.+) have\?*$/i, (msg) ->
115116
name = msg.match[2].trim()
116117
if name.toLowerCase() is 'i' then name = msg.message.user.name
117118
user = robot.brain.userForName(name)

test/auth-test.coffee

+44-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ describe "auth", ->
1717
@room.robot.brain.userForId "amy",
1818
name: "amy"
1919

20+
@room.robot.brain.userForId "jimmy jones",
21+
name: "jimmy jones"
22+
2023
afterEach ->
2124
@room.destroy()
2225

@@ -37,6 +40,13 @@ describe "auth", ->
3740
["hubot", "@alice OK, alice has the 'demo' role."]
3841
]
3942

43+
it "admin user successfully sets role for user with space in name", ->
44+
@room.user.say("alice", "hubot: jimmy jones has demo role").then =>
45+
expect(@room.messages).to.eql [
46+
["alice", "hubot: jimmy jones has demo role"]
47+
["hubot", "@alice OK, jimmy jones has the 'demo' role."]
48+
]
49+
4050

4151
it "fail to add admin role via command", ->
4252
@room.user.say("alice", "hubot: jimmy has admin role").then =>
@@ -70,6 +80,17 @@ describe "auth", ->
7080
["hubot", "@alice Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."]
7181
]
7282

83+
84+
it "admin user successfully removes role from user with space", ->
85+
@room.user.say("alice", "hubot: jimmy jones has demo role").then =>
86+
@room.user.say("alice", "hubot: jimmy jones doesn't have demo role").then =>
87+
expect(@room.messages).to.eql [
88+
["alice", "hubot: jimmy jones has demo role"]
89+
["hubot", "@alice OK, jimmy jones has the 'demo' role."]
90+
["alice", "hubot: jimmy jones doesn't have demo role"]
91+
["hubot", "@alice OK, jimmy jones doesn't have the 'demo' role."]
92+
]
93+
7394
context "what roles does <user> have", ->
7495
beforeEach ->
7596
@room.user.say("alice", "hubot: alice has demo role")
@@ -99,20 +120,28 @@ describe "auth", ->
99120
]
100121

101122
context "list assigned roles", ->
102-
beforeEach ->
103-
@room.user.say("alice", "hubot: alice has demo role").then =>
104-
@room.user.say("alice", "hubot: amy has test role").then =>
105-
@room.user.say "alice", "hubot: alice has test role"
106-
107123
it "successfully list assigned roles", ->
108-
@room.user.say("alice", "hubot: list assigned roles").then =>
124+
@room.user.say("alice", "hubot: alice has demo role").then =>
125+
@room.user.say("alice", "hubot: amy has test role").then =>
126+
@room.user.say("alice", "hubot: alice has test role").then =>
127+
@room.user.say("alice", "hubot: list assigned roles").then =>
128+
expect(@room.messages).to.eql [
129+
["alice", "hubot: alice has demo role"]
130+
["hubot", "@alice OK, alice has the 'demo' role."]
131+
["alice", "hubot: amy has test role"]
132+
["hubot", "@alice OK, amy has the 'test' role."]
133+
["alice", "hubot: alice has test role"]
134+
["hubot", "@alice OK, alice has the 'test' role."]
135+
["alice", "hubot: list assigned roles"]
136+
["hubot", "@alice The following roles are available: demo, test"]
137+
]
138+
139+
it "successfully lists roles of user with space in name", ->
140+
@room.user.say("alice", "hubot: jimmy jones has demo role").then =>
141+
@room.user.say("amy", "hubot: what roles does jimmy jones have?").then =>
109142
expect(@room.messages).to.eql [
110-
["alice", "hubot: alice has demo role"]
111-
["hubot", "@alice OK, alice has the 'demo' role."]
112-
["alice", "hubot: amy has test role"]
113-
["hubot", "@alice OK, amy has the 'test' role."]
114-
["alice", "hubot: alice has test role"]
115-
["hubot", "@alice OK, alice has the 'test' role."]
116-
["alice", "hubot: list assigned roles"]
117-
["hubot", "@alice The following roles are available: demo, test"]
118-
]
143+
["alice", "hubot: jimmy jones has demo role"]
144+
["hubot", "@alice OK, jimmy jones has the 'demo' role."]
145+
["amy", "hubot: what roles does jimmy jones have?"]
146+
["hubot", "@amy jimmy jones has the following roles: demo."]
147+
]

0 commit comments

Comments
 (0)