source: trunk/sources/HeuristicLab.Security.Core/3.2/SecurityCoreTest.cs @ 1729

Last change on this file since 1729 was 1729, checked in by svonolfe, 12 years ago

Fixed some bugs in the Security DAL (#597)

File size: 5.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.PluginInfrastructure;
6using HeuristicLab.Security.Contracts.BusinessObjects;
7
8namespace HeuristicLab.Security.Core {
9    [ClassInfo(Name = "Security Test App",
10        Description = "Test Application for the Security Service",
11        AutoRestart = true)]
12  class SecurityCoreTest : ApplicationBase {
13      private void testGroups() {
14        SecurityManager manager =
15          new SecurityManager();
16
17        User user = new User();
18        user.Login = "anna";
19        user.Password = "blubb";
20        user.Name = "Anna";
21        user = manager.AddNewUser(user);
22
23        User user2 = new User();
24        user2.Login = "stefan";
25        user2.Name = "Stefan";
26
27        UserGroup group = new UserGroup();
28        group.Name = "Students";
29
30        UserGroup group2 = new UserGroup();
31        group2.Name = "Lazy Students";
32
33        group.Members.Add(user);
34        group.Members.Add(group2);
35
36        group2.Members.Add(user2);
37
38        manager.AddNewUserGroup(group);
39
40        group = manager.GetUserGroupByName("Students");
41        group2 = manager.GetUserGroupByName("Lazy Students");
42
43        manager.RemoveUserGroup(group2.Id);
44
45        group = manager.GetUserGroupByName("Students");
46        group2 = manager.GetUserGroupByName("Lazy Students");
47
48        manager.RemoveUser(user.Id);
49
50        group = manager.GetUserGroupByName("Students");
51        manager.RemoveUserGroup(group.Id);
52
53        group = manager.GetUserGroupByName("Students");
54        manager.RemoveUser(user2.Id);
55      }
56
57      private void testPermissions() {
58        SecurityManager manager =
59           new SecurityManager();
60
61        PermissionManager permManager =
62          new PermissionManager();
63
64        Permission permission = new Permission();
65        permission.Name = "ADD_JOBS";
66        permission.Description = "Add jobs";
67        permission.Plugin = "TestPlugin";
68
69        manager.AddPermission(permission);
70
71        permission = manager.GetPermissionById(permission.Id);
72
73        User user = new User();
74        user.Login = "anna";
75        user.Password = "blubb";
76        user.Name = "Anna";
77        user = manager.AddNewUser(user);
78
79        manager.GrantPermission(user.Id, permission.Id, Guid.Empty);
80
81        Guid sessionId = permManager.Authenticate("anna", "blubb");
82
83        bool hasPermission =
84          permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
85
86        manager.RevokePermission(user.Id, permission.Id, Guid.Empty);
87
88        hasPermission =
89          permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
90
91        permManager.EndSession(sessionId);
92
93        manager.RemoveUser(user.Id);
94
95        manager.RemovePermission(permission.Id);
96
97        permission = manager.GetPermissionById(permission.Id);
98      }
99
100      private void testPermissionsGroup() {
101        SecurityManager manager =
102          new SecurityManager();
103
104        PermissionManager permManager =
105          new PermissionManager();
106
107        User user = new User();
108        user.Login = "anna";
109        user.Password = "blubb";
110        user.Name = "Anna";
111        user = manager.AddNewUser(user);
112
113        User user2 = new User();
114        user2.Login = "stefan";
115        user2.Password = "foo";
116        user2.Name = "Stefan";
117
118        UserGroup group = new UserGroup();
119        group.Name = "Students";
120
121        UserGroup group2 = new UserGroup();
122        group2.Name = "Lazy Students";
123
124        group.Members.Add(user);
125        group.Members.Add(group2);
126
127        group2.Members.Add(user2);
128
129        manager.AddNewUserGroup(group);
130
131        Permission permission = new Permission();
132        permission.Name = "ADD_JOBS";
133        permission.Description = "Add jobs";
134        permission.Plugin = "TestPlugin";
135
136        manager.AddPermission(permission);
137
138        manager.GrantPermission(group.Id, permission.Id, Guid.Empty);
139        manager.GrantPermission(group2.Id, permission.Id, Guid.Empty);
140
141        Guid sessionId = permManager.Authenticate("anna", "blubb");
142
143        bool hasPermission =
144          permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
145
146        Guid sessionId2 = permManager.Authenticate("stefan", "foo");
147
148        hasPermission =
149          permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
150
151        manager.RevokePermission(group.Id, permission.Id, Guid.Empty);
152
153        hasPermission =
154          permManager.CheckPermission(sessionId, permission.Id, Guid.Empty);
155
156        hasPermission =
157          permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
158
159        manager.RevokePermission(group2.Id, permission.Id, Guid.Empty);
160
161        hasPermission =
162          permManager.CheckPermission(sessionId2, permission.Id, Guid.Empty);
163
164        permManager.EndSession(sessionId);
165        permManager.EndSession(sessionId2);
166
167        manager.RemoveUser(user.Id);
168        manager.RemoveUser(user2.Id);
169        manager.RemoveUserGroup(group.Id);
170        manager.RemoveUserGroup(group2.Id);
171
172        manager.RemovePermission(permission.Id);                       
173      }
174     
175      public override void Run() {
176        testPermissionsGroup();
177      }
178    }
179}
Note: See TracBrowser for help on using the repository browser.