DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
UserManagementTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 using System;
16 
17 using NUnit.Framework;
18 
19 namespace Deveel.Data.Security {
20  [TestFixture]
22  protected override ISession CreateAdminSession(IDatabase database) {
23  var testName = TestContext.CurrentContext.Test.Name;
24  if (testName != "CreateUser") {
25  using (var session = base.CreateAdminSession(database)) {
26  using (var query = session.CreateQuery()) {
27  query.CreateUser("tester", "123456789");
28  query.Commit();
29  }
30  }
31  }
32  return base.CreateAdminSession(database);
33  }
34 
35  [Test]
36  public void CreateUser() {
37  User user = null;
38  Assert.DoesNotThrow(() => user = Query.CreateUser("tester", "123456"));
39  Assert.IsNotNull(user);
40 
41  Assert.AreEqual("tester", user.Name);
42 
43  bool exists = false;
44  Assert.DoesNotThrow(() => exists = Query.UserExists("tester"));
45  Assert.IsTrue(exists);
46  }
47 
48  [Test]
49  public void Authenticate_Success() {
50  User user = null;
51 
52  Assert.DoesNotThrow(() => user = Database.Authenticate("tester", "123456789"));
53  Assert.IsNotNull(user);
54  Assert.AreEqual("tester", user.Name);
55  }
56 
57  [Test]
58  public void Authenticate_Fail() {
59  User user = null;
60 
61  Assert.Throws<SecurityException>(() => user = Database.Authenticate("test2", "12545587"));
62  Assert.IsNull(user);
63  }
64 
65  [Test]
66  public void CreateExistingUser() {
67  bool exists = false;
68  Assert.DoesNotThrow(() => exists = Query.UserExists("tester"));
69  Assert.IsTrue(exists);
70 
71  Assert.Throws<SecurityException>(() => Query.CreateUser("tester", "123456789"));
72  }
73 
74  [Test]
75  public void DropUser() {
76  Assert.DoesNotThrow(() => Query.DeleteUser("tester"));
77 
78  bool exists = false;
79  Assert.DoesNotThrow(() => Query.UserExists("tester"));
80  Assert.IsFalse(exists);
81  }
82 
83  [Test]
84  public void AdminChangesUserPassword() {
85  Assert.DoesNotThrow(() => Query.AlterUserPassword("tester", "0123456789"));
86  }
87 
88  [Test]
89  public void SetUserGroups() {
90  Assert.DoesNotThrow(() => Query.AddUserToGroup("tester", "test_group"));
91  Assert.DoesNotThrow(() => Query.AddUserToGroup("tester", SystemGroups.UserManagerGroup));
92 
93  User user = null;
94  Assert.DoesNotThrow(() => user = Query.GetUser("tester"));
95  Assert.IsNotNull(user);
96 
97  string[] userGroups = null;
98  Assert.DoesNotThrow(() => userGroups = Query.GetGroupsUserBelongsTo(user.Name));
99  Assert.IsNotNull(userGroups);
100  Assert.Contains("test_group", userGroups);
101  Assert.Contains(SystemGroups.UserManagerGroup, userGroups);
102 
103  Assert.IsTrue(Query.UserBelongsToGroup("tester", "test_group"));
104  }
105 
106  [Test]
107  public void LockUser() {
108  Query.SetUserStatus("tester", UserStatus.Locked);
109 
110  UserStatus status = new UserStatus();
111  Assert.DoesNotThrow(() => status = Query.GetUserStatus("tester"));
112  Assert.AreEqual(UserStatus.Locked, status);
113  }
114  }
115 }
static User CreateUser(this IQuery query, string userName, string password)
static void AddUserToGroup(this IQuery queryContext, string username, string group, bool asAdmin=false)
static void AlterUserPassword(this IQuery queryContext, string username, string password)
void Commit()
Commits the latest changes made by the user in the session.
static string[] GetGroupsUserBelongsTo(this IQuery queryContext, string username)
The default implementation of a database in a system.
Definition: Database.cs:38
The representation of a single database in the system.
Definition: IDatabase.cs:40
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
const string UserManagerGroup
The name of the user manager group.
Definition: SystemGroups.cs:55
static User GetUser(this IQuery query, string userName)
static bool UserExists(this IQuery query, string userName)
static bool UserBelongsToGroup(this IQuery queryContext, string group)
static bool DeleteUser(this IQuery query, string userName)
Provides the information for a user in a database system
Definition: User.cs:27
static void SetUserStatus(this IQuery queryContext, string username, UserStatus status)
override ISession CreateAdminSession(IDatabase database)
static UserStatus GetUserStatus(this IQuery queryContext, string userName)
string Name
Gets the name that uniquely identify a user within a database system.
Definition: User.cs:57