1 | using Service.Provider;
|
---|
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Data.SqlClient;
|
---|
6 | namespace UnitTests {
|
---|
7 | /// <summary>
|
---|
8 | ///This is a test class for HeuristicLabRoleProviderTest and is intended
|
---|
9 | ///to contain all HeuristicLabRoleProviderTest Unit Tests
|
---|
10 | ///</summary>
|
---|
11 | [TestClass()]
|
---|
12 | public class HeuristicLabRoleProviderTest : AbstractHeuristicLabTest {
|
---|
13 | private TestContext testContextInstance;
|
---|
14 | private const string TEST_ROLE_NAME = "testRole";
|
---|
15 | private const string TEST_USER_NAME = "testUser";
|
---|
16 | /// <summary>
|
---|
17 | ///Gets or sets the test context which provides
|
---|
18 | ///information about and functionality for the current test run.
|
---|
19 | ///</summary>
|
---|
20 | public TestContext TestContext {
|
---|
21 | get {
|
---|
22 | return testContextInstance;
|
---|
23 | }
|
---|
24 | set {
|
---|
25 | testContextInstance = value;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | #region Additional test attributes
|
---|
30 | //
|
---|
31 | //You can use the following additional attributes as you write your tests:
|
---|
32 | //
|
---|
33 | //Use ClassInitialize to run code before running the first test in the class
|
---|
34 | //[ClassInitialize()]
|
---|
35 | //public static void MyClassInitialize(TestContext testContext)
|
---|
36 | //{
|
---|
37 | //}
|
---|
38 | //
|
---|
39 | //Use ClassCleanup to run code after all tests in a class have run
|
---|
40 | //[ClassCleanup()]
|
---|
41 | //public static void MyClassCleanup()
|
---|
42 | //{
|
---|
43 | //}
|
---|
44 | //
|
---|
45 | //Use TestInitialize to run code before running each test
|
---|
46 | //[TestInitialize()]
|
---|
47 | //public void MyTestInitialize()
|
---|
48 | //{
|
---|
49 | //}
|
---|
50 | //
|
---|
51 | //Use TestCleanup to run code after each test has run
|
---|
52 | //[TestCleanup()]
|
---|
53 | //public void MyTestCleanup()
|
---|
54 | //{
|
---|
55 | //}
|
---|
56 | //
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | ///A test for ApplicationName
|
---|
62 | ///</summary>
|
---|
63 | [TestMethod()]
|
---|
64 | public void ApplicationNameTest() {
|
---|
65 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
66 | string expected = "JavaIsEvenCooler";
|
---|
67 | string actual;
|
---|
68 | target.ApplicationName = expected;
|
---|
69 | actual = target.ApplicationName;
|
---|
70 | Assert.AreEqual(expected, actual);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | ///tests if the RoleExits method works --> test is done in a positiv and negativ way
|
---|
75 | ///</summary>
|
---|
76 | [TestMethod()]
|
---|
77 | public void RoleExistsTest() {
|
---|
78 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
79 | Persistence.HeuristicLabRole role = new Persistence.HeuristicLabRole();
|
---|
80 | role.RoleName = TEST_ROLE_NAME;
|
---|
81 | db.HeuristicLabRole.InsertOnSubmit((Persistence.HeuristicLabRole)role);
|
---|
82 | db.SubmitChanges();
|
---|
83 | Assert.IsTrue(target.RoleExists(TEST_ROLE_NAME));
|
---|
84 | Assert.IsFalse(target.RoleExists(TEST_ROLE_NAME + TEST_ROLE_NAME));
|
---|
85 | }
|
---|
86 |
|
---|
87 | /// <summary>
|
---|
88 | ///A test for RemoveUsersFromRoles
|
---|
89 | ///</summary>
|
---|
90 | [TestMethod()]
|
---|
91 | public void RemoveUsersFromRolesTest() {
|
---|
92 |
|
---|
93 | //creates users dkhan,hmayer,bfarka (with AddUsersToRolesTest())
|
---|
94 | //and add all these users to groups admin and users
|
---|
95 | AddUsersToRolesTest();
|
---|
96 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
97 | string[] users = new string[2];
|
---|
98 | string[] roles = new string[1];
|
---|
99 |
|
---|
100 | //remove hmayer from role admin
|
---|
101 | users[0] = "hmayr";
|
---|
102 | roles[0] = "admin";
|
---|
103 |
|
---|
104 |
|
---|
105 | //before removing, check if all three users exits correctly in admin group
|
---|
106 | Assert.IsTrue((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
|
---|
107 | Assert.IsTrue((target.GetRolesForUser("dkhan").ToList()).Contains(roles[0]));
|
---|
108 | Assert.IsTrue((target.GetRolesForUser("bfarka").ToList()).Contains(roles[0]));
|
---|
109 |
|
---|
110 | //remmove hmayr from admin group
|
---|
111 | target.RemoveUsersFromRoles(users, roles);
|
---|
112 |
|
---|
113 | //final check
|
---|
114 | Assert.IsTrue((target.GetRolesForUser("dkhan").ToList()).Contains(roles[0]));
|
---|
115 | Assert.IsTrue((target.GetRolesForUser("bfarka").ToList()).Contains(roles[0]));
|
---|
116 | Assert.IsFalse((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
|
---|
117 |
|
---|
118 | }
|
---|
119 |
|
---|
120 | /// <summary>
|
---|
121 | /// test if user is in Role (positive and negative Assertion)
|
---|
122 | ///</summary>
|
---|
123 | [TestMethod()]
|
---|
124 | public void IsUserInRoleTest() {
|
---|
125 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
|
---|
126 | Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
|
---|
127 | List<string> roles = new List<string>();
|
---|
128 | users.Add("mholper", new Persistence.HeuristicLabUser("mholper", "foo", "password", "comment"));
|
---|
129 |
|
---|
130 | roles.Add("admin");
|
---|
131 | roles.Add("users");
|
---|
132 | foreach (string role in roles) {
|
---|
133 | target.CreateRole(role);
|
---|
134 | }
|
---|
135 | foreach (Persistence.HeuristicLabUser user in users.Values) {
|
---|
136 | db.HeuristicLabUsers.InsertOnSubmit(user);
|
---|
137 | }
|
---|
138 | db.SubmitChanges();
|
---|
139 | string[] rolesToTest = new string[1];
|
---|
140 | rolesToTest[0] = "admin";
|
---|
141 | target.AddUsersToRoles(users.Keys.ToArray(), rolesToTest); // roles.ToArray());
|
---|
142 | Assert.IsTrue(target.IsUserInRole("mholper", "admin"));
|
---|
143 | Assert.IsFalse(target.IsUserInRole("mholper", "user"));
|
---|
144 | }
|
---|
145 |
|
---|
146 | /// <summary>
|
---|
147 | ///A test for GetUsersInRole
|
---|
148 | ///</summary>
|
---|
149 | [TestMethod()]
|
---|
150 | public void GetUsersInRoleTest() {
|
---|
151 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
|
---|
152 | string dummyRole = "dummyRole";
|
---|
153 | Assert.IsTrue(target.GetUsersInRole(null).Length == 0);
|
---|
154 | Assert.IsTrue(target.GetUsersInRole(dummyRole).Length == 0);
|
---|
155 | Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo1", "foo", "foo");
|
---|
156 | Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo2", "foo", "foo");
|
---|
157 | db.HeuristicLabUsers.InsertOnSubmit(user);
|
---|
158 | db.HeuristicLabUsers.InsertOnSubmit(user2);
|
---|
159 | db.SubmitChanges();
|
---|
160 | target.CreateRole("testRole1");
|
---|
161 | target.CreateRole("testRole2");
|
---|
162 | List<string> users = new List<string>();
|
---|
163 | List<string> roles = new List<string>();
|
---|
164 | roles.Add("testRole1");
|
---|
165 | roles.Add("testRole2");
|
---|
166 | users.Add("dummyUser");
|
---|
167 |
|
---|
168 | //--> dummyUser get Role testRole1+testRole2
|
---|
169 | target.AddUsersToRoles(users.ToArray(), roles.ToArray());
|
---|
170 | string[] usersForRole = target.GetUsersInRole("testRole2");
|
---|
171 | Assert.IsTrue(usersForRole.Length == 1);
|
---|
172 | Assert.IsTrue(usersForRole.Contains("dummyUser"));
|
---|
173 |
|
---|
174 | //--> dummyUser2 get onl Role testRole1
|
---|
175 | roles.Remove("testRole2");
|
---|
176 | users.Remove("dummyUser");
|
---|
177 | users.Add("dummyUser2");
|
---|
178 | target.AddUsersToRoles(users.ToArray(), roles.ToArray());
|
---|
179 |
|
---|
180 | usersForRole = target.GetUsersInRole("testRole1");
|
---|
181 | Assert.IsTrue(usersForRole.Length == 2);
|
---|
182 | Assert.IsTrue(usersForRole.Contains("dummyUser"));
|
---|
183 | Assert.IsTrue(usersForRole.Contains("dummyUser2"));
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | /// <summary>
|
---|
188 | ///A test for GetRolesForUser
|
---|
189 | ///</summary>
|
---|
190 | [TestMethod()]
|
---|
191 | public void GetRolesForUserTest() {
|
---|
192 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
|
---|
193 | string dummyUser = "dummyUser";
|
---|
194 | Assert.IsTrue(target.GetRolesForUser(null).Length == 0);
|
---|
195 | Assert.IsTrue(target.GetRolesForUser(dummyUser).Length == 0);
|
---|
196 | Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo", "foo", "foo");
|
---|
197 | Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo2", "foo", "foo");
|
---|
198 | db.HeuristicLabUsers.InsertOnSubmit(user);
|
---|
199 | db.HeuristicLabUsers.InsertOnSubmit(user2);
|
---|
200 | db.SubmitChanges();
|
---|
201 | target.CreateRole("testRole1");
|
---|
202 | target.CreateRole("testRole2");
|
---|
203 | List<string> users = new List<string>();
|
---|
204 | List<string> roles = new List<string>();
|
---|
205 | users.Add("dummyUser");
|
---|
206 | users.Add("dummyUser2");
|
---|
207 | roles.Add("testRole1");
|
---|
208 | target.AddUsersToRoles(users.ToArray(), roles.ToArray());
|
---|
209 | users.Remove("dummyUser2");
|
---|
210 | roles.Add("testRole2");
|
---|
211 | roles.Remove("testRole1");
|
---|
212 | target.AddUsersToRoles(users.ToArray(), roles.ToArray());
|
---|
213 | string[] rolesForUser = target.GetRolesForUser("dummyUser");
|
---|
214 | Assert.IsTrue(rolesForUser.Length == 2);
|
---|
215 | Assert.IsTrue(rolesForUser.Contains("testRole1"));
|
---|
216 | Assert.IsTrue(rolesForUser.Contains("testRole2"));
|
---|
217 |
|
---|
218 | rolesForUser = target.GetRolesForUser("dummyUser2");
|
---|
219 | Assert.IsTrue(rolesForUser.Length == 1);
|
---|
220 | Assert.IsTrue(rolesForUser.Contains("testRole1"));
|
---|
221 |
|
---|
222 |
|
---|
223 |
|
---|
224 | }
|
---|
225 |
|
---|
226 | /// <summary>
|
---|
227 | ///A test for GetAllRoles
|
---|
228 | ///</summary>
|
---|
229 | [TestMethod()]
|
---|
230 | public void GetAllRolesTest() {
|
---|
231 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
232 | List<string> roleNames = new List<string>();
|
---|
233 | roleNames.Add("Pascal");
|
---|
234 | roleNames.Add("Java");
|
---|
235 | roleNames.Add("c++");
|
---|
236 | roleNames.Add("VisalBasic");
|
---|
237 | foreach (string s in roleNames) {
|
---|
238 | target.CreateRole(s);
|
---|
239 | }
|
---|
240 | target.CreateRole(null);
|
---|
241 | string[] roles = target.GetAllRoles();
|
---|
242 | foreach (string role in roles) {
|
---|
243 | Assert.IsTrue(roleNames.Remove(role));
|
---|
244 | }
|
---|
245 | Assert.IsTrue(roleNames.Count == 0);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /// <summary>
|
---|
249 | ///A test for FindUsersInRole
|
---|
250 | ///</summary>
|
---|
251 | [TestMethod()]
|
---|
252 | public void FindUsersInRoleTest() {
|
---|
253 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
254 | Assert.IsTrue(target.FindUsersInRole(null, null).Length == 0);
|
---|
255 | Assert.IsTrue(target.FindUsersInRole("dummyRole", null).Length == 0);
|
---|
256 | Assert.IsTrue(target.FindUsersInRole(null, "dummyUser").Length == 0);
|
---|
257 | Persistence.HeuristicLabUser user = new Persistence.HeuristicLabUser("dummyUser", "foo", "foo", "foo");
|
---|
258 | Persistence.HeuristicLabUser user2 = new Persistence.HeuristicLabUser("dummyUser2", "foo2", "foo", "foo");
|
---|
259 | db.HeuristicLabUsers.InsertOnSubmit(user);
|
---|
260 | db.HeuristicLabUsers.InsertOnSubmit(user2);
|
---|
261 | db.SubmitChanges();
|
---|
262 | target.CreateRole("testRole");
|
---|
263 | Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 0);
|
---|
264 | Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 0);
|
---|
265 | Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
|
---|
266 |
|
---|
267 | target.AddUsersToRoles(new string[] { "dummyUser", "dummyUser2" }, new string[] { "testRole" });
|
---|
268 | Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser").Length == 2);
|
---|
269 | Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser2").Length == 1);
|
---|
270 | Assert.IsTrue(target.FindUsersInRole("testRole", "dummyUser3").Length == 0);
|
---|
271 |
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 | /// <summary>
|
---|
276 | ///A test for DeleteRole
|
---|
277 | ///</summary>
|
---|
278 | [TestMethod()]
|
---|
279 | public void DeleteRoleTest() {
|
---|
280 |
|
---|
281 | //creates users dkhan,hmayer,bfarka (with AddUsersToRolesTest())
|
---|
282 | //and add all these users to groups admin and users
|
---|
283 | AddUsersToRolesTest();
|
---|
284 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
285 | string[] users = new string[2];
|
---|
286 | string[] roles = new string[1];
|
---|
287 |
|
---|
288 | //remove hmayer from role admin
|
---|
289 | users[0] = "hmayr";
|
---|
290 | roles[0] = "admin";
|
---|
291 |
|
---|
292 | //before removing, check if all three users exits correctly in admin group
|
---|
293 | Assert.IsTrue((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
|
---|
294 | Assert.IsTrue((target.GetRolesForUser("dkhan").ToList()).Contains(roles[0]));
|
---|
295 | Assert.IsTrue((target.GetRolesForUser("bfarka").ToList()).Contains(roles[0]));
|
---|
296 |
|
---|
297 | //try to delete Role admin (with user hmayr from admin group exists)
|
---|
298 | Assert.IsFalse(target.DeleteRole(roles[0], true));
|
---|
299 |
|
---|
300 | Assert.IsTrue(target.IsUserInRole(users[0], roles[0]));
|
---|
301 |
|
---|
302 | //final populateRole if false
|
---|
303 | //try to delete Role admin (with user hmayr from admin group exists)
|
---|
304 | Assert.IsTrue(target.DeleteRole(roles[0], false));
|
---|
305 | Assert.IsFalse(target.IsUserInRole(users[0], roles[0]));
|
---|
306 | Assert.IsFalse((target.GetRolesForUser("hmayr").ToList()).Contains(roles[0]));
|
---|
307 | }
|
---|
308 |
|
---|
309 | /// <summary>
|
---|
310 | ///A test for CreateRole
|
---|
311 | ///</summary>
|
---|
312 | [TestMethod()]
|
---|
313 | public void CreateRoleTest() {
|
---|
314 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
315 | target.CreateRole("role1");
|
---|
316 | target.CreateRole("role2");
|
---|
317 | target.CreateRole("role3", true);
|
---|
318 | Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role1" && r.IsPermission == false) == 1);
|
---|
319 | Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role2" && r.IsPermission == false) == 1);
|
---|
320 | Assert.IsTrue(db.HeuristicLabRole.Count(r => r.RoleName == "role3" && r.IsPermission == true) == 1);
|
---|
321 | }
|
---|
322 |
|
---|
323 | protected int getUserRolesCount() {
|
---|
324 | return db.HeuristicLabUserRole.Count();
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | /// <summary>
|
---|
329 | ///A test for AddUsersToRoles
|
---|
330 | ///</summary>
|
---|
331 | [TestMethod()]
|
---|
332 | public void AddUsersToRolesTest() {
|
---|
333 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider(); // TODO: Initialize to an appropriate value
|
---|
334 | Dictionary<string, Persistence.HeuristicLabUser> users = new Dictionary<string, Persistence.HeuristicLabUser>();
|
---|
335 | List<string> roles = new List<string>();
|
---|
336 | users.Add("dkhan", new Persistence.HeuristicLabUser("dkhan", "foo1", "password", "comment"));
|
---|
337 | users.Add("hmayr", new Persistence.HeuristicLabUser("hmayr", "foo2", "password", "comment"));
|
---|
338 | users.Add("bfarka", new Persistence.HeuristicLabUser("bfarka", "foo3", "password", "comment"));
|
---|
339 |
|
---|
340 | roles.Add("admin");
|
---|
341 | roles.Add("users");
|
---|
342 | //testing to create roles with users that doesn't exists
|
---|
343 | target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
|
---|
344 | Assert.IsTrue(getUserRolesCount() == 0);
|
---|
345 |
|
---|
346 | foreach (string role in roles) {
|
---|
347 | target.CreateRole(role);
|
---|
348 | }
|
---|
349 | target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
|
---|
350 | Assert.IsTrue(getUserRolesCount() == 0);
|
---|
351 | foreach (Persistence.HeuristicLabUser user in users.Values) {
|
---|
352 | db.HeuristicLabUsers.InsertOnSubmit(user);
|
---|
353 | }
|
---|
354 | db.SubmitChanges();
|
---|
355 | target.AddUsersToRoles(users.Keys.ToArray(), roles.ToArray());
|
---|
356 | Assert.IsTrue(getUserRolesCount() == (roles.Count * users.Count));
|
---|
357 |
|
---|
358 | }
|
---|
359 |
|
---|
360 | /// <summary>
|
---|
361 | ///A test for HeuristicLabRoleProvider Constructor
|
---|
362 | ///</summary>
|
---|
363 | [TestMethod()]
|
---|
364 | public void HeuristicLabRoleProviderConstructorTest() {
|
---|
365 | HeuristicLabRoleProvider target = new HeuristicLabRoleProvider();
|
---|
366 | Assert.IsNotNull(target);
|
---|
367 | }
|
---|
368 | [TestMethod()]
|
---|
369 | public void NoDuplicateRoleTest() {
|
---|
370 | try {
|
---|
371 | Persistence.HeuristicLabRole role = new Persistence.HeuristicLabRole();
|
---|
372 | role.RoleName = "role1";
|
---|
373 | Persistence.HeuristicLabRole role1 = new Persistence.HeuristicLabRole();
|
---|
374 | role1.RoleName = "role1";
|
---|
375 | db.HeuristicLabRole.InsertOnSubmit(role);
|
---|
376 | db.HeuristicLabRole.InsertOnSubmit(role1);
|
---|
377 | db.SubmitChanges();
|
---|
378 | Assert.Fail();
|
---|
379 | }
|
---|
380 | catch (SqlException) {
|
---|
381 | //swallowing Exception because it is expected that a SQL Exception is thrown
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|