|
1 |
| -expect = require('chai').expect |
2 |
| -path = require 'path' |
3 |
| - |
4 |
| -Robot = require 'hubot/src/robot' |
5 |
| -TextMessage = require('hubot/src/message').TextMessage |
6 |
| - |
7 |
| -describe 'auth', -> |
8 |
| - robot = {} |
9 |
| - admin_user = {} |
10 |
| - role_user = {} |
11 |
| - anon_user = {} |
12 |
| - adapter = {} |
13 |
| - |
14 |
| - beforeEach (done) -> |
15 |
| - process.env.HUBOT_AUTH_ADMIN = "1" |
16 |
| - |
17 |
| - # Create new robot, without http, using mock adapter |
18 |
| - robot = new Robot null, "mock-adapter", false |
19 |
| - |
20 |
| - robot.adapter.on "connected", -> |
21 |
| - |
22 |
| - # load the module under test and configure it for the |
23 |
| - # robot. This is in place of external-scripts |
24 |
| - require("../src/auth")(robot) |
25 |
| - |
26 |
| - admin_user = robot.brain.userForId "1", { |
27 |
| - name: "admin-user" |
28 |
| - room: "#test" |
29 |
| - } |
30 |
| - |
31 |
| - role_user = robot.brain.userForId "2", { |
32 |
| - name: "role-user" |
33 |
| - room: "#test" |
34 |
| - } |
35 |
| - |
36 |
| - anon_user = robot.brain.userForId "3", { |
37 |
| - name: "anon-user" |
38 |
| - room: "#test" |
39 |
| - } |
40 |
| - |
41 |
| - adapter = robot.adapter |
42 |
| - |
43 |
| - robot.run() |
44 |
| - |
45 |
| - done() |
46 |
| - |
47 |
| - afterEach (done) -> |
48 |
| - robot.shutdown() |
49 |
| - done() |
50 |
| - |
51 |
| - it 'list admin users', (done) -> |
52 |
| - adapter.on "reply", (envelope, strings) -> |
53 |
| - expect(strings[0]).to.match /admin-user/i |
54 |
| - done() |
55 |
| - |
56 |
| - adapter.receive(new TextMessage admin_user, "hubot: who has admin role?") |
57 |
| - |
58 |
| - it 'list admin users using non-admin user', (done) -> |
59 |
| - adapter.on "reply", (envelope, strings) -> |
60 |
| - expect(strings[0]).to.match /admin-user/i |
61 |
| - done() |
62 |
| - |
63 |
| - adapter.receive(new TextMessage anon_user, "hubot: who has admin role?") |
64 |
| - |
65 |
| - it 'anon user fails to set role', (done) -> |
66 |
| - adapter.on "reply", (envelope, strings) -> |
67 |
| - expect(strings[0]).to.match /only admins can assign roles/i |
68 |
| - done() |
69 |
| - |
70 |
| - adapter.receive(new TextMessage anon_user, "hubot: role-user has demo role") |
71 |
| - |
72 |
| - it 'admin user successfully sets role', (done) -> |
73 |
| - adapter.on "reply", (envelope, strings) -> |
74 |
| - expect(strings[0]).to.match /role-user has the 'demo' role/i |
75 |
| - done() |
76 |
| - |
77 |
| - adapter.receive(new TextMessage admin_user, "hubot: role-user has demo role") |
78 |
| - |
79 |
| - it 'admin user successfully sets role in the first-person', (done) -> |
80 |
| - adapter.on "reply", (envelope, strings) -> |
81 |
| - expect(strings[0]).to.match /admin-user has the 'demo' role/i |
82 |
| - done() |
83 |
| - |
84 |
| - adapter.receive(new TextMessage admin_user, "hubot: I have demo role") |
85 |
| - |
86 |
| - it 'fail to add admin role via command', (done) -> |
87 |
| - adapter.on "reply", (envelope, strings) -> |
88 |
| - expect(strings[0]).to.match /sorry/i |
89 |
| - done() |
90 |
| - |
91 |
| - adapter.receive(new TextMessage admin_user, "hubot: role-user has admin role") |
92 |
| - |
93 |
| - it 'fail to remove admin role via command', (done) -> |
94 |
| - adapter.on "reply", (envelope, strings) -> |
95 |
| - expect(strings[0]).to.match /sorry/i |
96 |
| - done() |
97 |
| - |
98 |
| - adapter.receive(new TextMessage admin_user, "hubot: role-user doesn't have admin role") |
99 |
| - |
100 |
| - it 'admin user successfully removes role in the first-person', (done) -> |
101 |
| - adapter.receive(new TextMessage admin_user, "hubot: admin-user has demo role") |
102 |
| - |
103 |
| - adapter.on "reply", (envelope, strings) -> |
104 |
| - if strings[0].match /OK, admin-user has .*demo/i |
105 |
| - return |
106 |
| - |
107 |
| - expect(strings[0]).to.match /ha(s|ve) the 'demo' role/i |
108 |
| - done() |
109 |
| - |
110 |
| - adapter.receive(new TextMessage admin_user, "hubot: I don't have demo role") |
111 |
| - |
112 |
| - it 'successfully list multiple roles of admin user', (done) -> |
113 |
| - adapter.receive(new TextMessage admin_user, "hubot: admin-user has demo role") |
114 |
| - |
115 |
| - adapter.on "reply", (envelope, strings) -> |
116 |
| - if strings[0].match /OK, admin-user has .*demo/i |
117 |
| - return |
118 |
| - |
119 |
| - expect(strings[0]).to.match(/following roles: .*admin/) |
120 |
| - expect(strings[0]).to.match(/following roles: .*demo/) |
121 |
| - done() |
122 |
| - |
123 |
| - adapter.receive(new TextMessage anon_user, "hubot: what roles does admin-user have?") |
124 |
| - |
125 |
| - it 'successfully list assigned roles', (done) -> |
126 |
| - adapter.receive(new TextMessage admin_user, "hubot: admin-user has demo role") |
127 |
| - adapter.receive(new TextMessage admin_user, "hubot: anon-user has test role") |
128 |
| - adapter.receive(new TextMessage admin_user, "hubot: admin-user has test role") |
129 |
| - |
130 |
| - adapter.on "reply", (envelope, strings) -> |
131 |
| - if strings[0].match /OK, .* has .* role/i |
132 |
| - return |
133 |
| - |
134 |
| - |
135 |
| - expect(strings[0]).to.match(/following roles .*:.*demo.*test/) |
136 |
| - done() |
137 |
| - |
138 |
| - adapter.receive(new TextMessage admin_user, "hubot: list assigned roles") |
| 1 | +Helper = require("hubot-test-helper") |
| 2 | +helper = new Helper("../src") |
| 3 | + |
| 4 | +expect = require("chai").expect |
| 5 | + |
| 6 | +describe "auth", -> |
| 7 | + |
| 8 | + beforeEach -> |
| 9 | + process.env.HUBOT_AUTH_ADMIN = "alice" |
| 10 | + @room = helper.createRoom() |
| 11 | + @room.robot.brain.userForId "alice", |
| 12 | + name: "alice" |
| 13 | + |
| 14 | + @room.robot.brain.userForId "jimmy", |
| 15 | + name: "jimmy" |
| 16 | + |
| 17 | + @room.robot.brain.userForId "amy", |
| 18 | + name: "amy" |
| 19 | + |
| 20 | + afterEach -> |
| 21 | + @room.destroy() |
| 22 | + |
| 23 | + context "<user> has <role> role", -> |
| 24 | + |
| 25 | + it "admin user successfully sets role", -> |
| 26 | + @room.user.say("alice", "hubot: jimmy has demo role").then => |
| 27 | + expect(@room.messages).to.eql [ |
| 28 | + ["alice", "hubot: jimmy has demo role"] |
| 29 | + ["hubot", "@alice OK, jimmy has the 'demo' role."] |
| 30 | + ] |
| 31 | + |
| 32 | + |
| 33 | + it "admin user successfully sets role in the first-person", -> |
| 34 | + @room.user.say("alice", "hubot: I have demo role").then => |
| 35 | + expect(@room.messages).to.eql [ |
| 36 | + ["alice", "hubot: I have demo role"] |
| 37 | + ["hubot", "@alice OK, alice has the 'demo' role."] |
| 38 | + ] |
| 39 | + |
| 40 | + |
| 41 | + it "fail to add admin role via command", -> |
| 42 | + @room.user.say("alice", "hubot: jimmy has admin role").then => |
| 43 | + expect(@room.messages).to.eql [ |
| 44 | + ["alice", "hubot: jimmy has admin role"] |
| 45 | + ["hubot", "@alice Sorry, the 'admin' role can only be defined in the HUBOT_AUTH_ADMIN env variable."] |
| 46 | + ] |
| 47 | + |
| 48 | + it "anon user fails to set role", -> |
| 49 | + @room.user.say("amy", "hubot: jimmy has demo role").then => |
| 50 | + expect(@room.messages).to.eql [ |
| 51 | + ["amy", "hubot: jimmy has demo role"] |
| 52 | + ["hubot", "@amy Sorry, only admins can assign roles."] |
| 53 | + ] |
| 54 | + |
| 55 | + context "<user> doesn't have <role> role", -> |
| 56 | + it "admin user successfully removes role in the first-person", -> |
| 57 | + @room.user.say("alice", "hubot: alice has demo role").then => |
| 58 | + @room.user.say("alice", "hubot: I don't have demo role").then => |
| 59 | + expect(@room.messages).to.eql [ |
| 60 | + ["alice", "hubot: alice has demo role"] |
| 61 | + ["hubot", "@alice OK, alice has the 'demo' role."] |
| 62 | + ["alice", "hubot: I don't have demo role"] |
| 63 | + ["hubot", "@alice OK, alice doesn't have the 'demo' role."] |
| 64 | + ] |
| 65 | + |
| 66 | + it "fail to remove admin role via command", -> |
| 67 | + @room.user.say("alice", "hubot: jimmy doesn't have admin role").then => |
| 68 | + expect(@room.messages).to.eql [ |
| 69 | + ["alice", "hubot: jimmy doesn't have admin role"] |
| 70 | + ["hubot", "@alice Sorry, the 'admin' role can only be removed from the HUBOT_AUTH_ADMIN env variable."] |
| 71 | + ] |
| 72 | + |
| 73 | + context "what roles does <user> have", -> |
| 74 | + beforeEach -> |
| 75 | + @room.user.say("alice", "hubot: alice has demo role") |
| 76 | + |
| 77 | + it "successfully list multiple roles of admin user", -> |
| 78 | + @room.user.say("amy", "hubot: what roles does alice have?").then => |
| 79 | + expect(@room.messages).to.eql [ |
| 80 | + ["alice", "hubot: alice has demo role"] |
| 81 | + ["hubot", "@alice OK, alice has the 'demo' role."] |
| 82 | + ["amy", "hubot: what roles does alice have?"] |
| 83 | + ["hubot", "@amy alice has the following roles: admin, demo."] |
| 84 | + ] |
| 85 | + |
| 86 | + context "who has <role> role", -> |
| 87 | + it "list admin users", -> |
| 88 | + @room.user.say("alice", "hubot: who has admin role?").then => |
| 89 | + expect(@room.messages).to.eql [ |
| 90 | + ["alice", "hubot: who has admin role?"] |
| 91 | + ["hubot", "@alice The following people have the 'admin' role: alice"] |
| 92 | + ] |
| 93 | + |
| 94 | + it "list admin users using non-admin user", -> |
| 95 | + @room.user.say("amy", "hubot: who has admin role?").then => |
| 96 | + expect(@room.messages).to.eql [ |
| 97 | + ["amy", "hubot: who has admin role?"] |
| 98 | + ["hubot", "@amy The following people have the 'admin' role: alice"] |
| 99 | + ] |
| 100 | + |
| 101 | + 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 | + |
| 107 | + it "successfully list assigned roles", -> |
| 108 | + @room.user.say("alice", "hubot: list assigned roles").then => |
| 109 | + 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 | + ] |
0 commit comments