DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
GrantTests.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 using System.Linq;
17 
18 using Deveel.Data.Security;
19 
20 using NUnit.Framework;
21 
22 namespace Deveel.Data.Sql.Statements {
23  [TestFixture]
24  public class GrantTests : ContextBasedTest {
25  protected override IQuery CreateQuery(ISession session) {
26  var query = base.CreateQuery(session);
27  query.CreateUser("test_user", "12345");
28  return query;
29  }
30 
31  [Test]
33  const string sql = "GRANT SELECT, DELETE, UPDATE PRIVILEGE ON test_table TO test_user";
34 
35  var statements = SqlStatement.Parse(sql);
36  Assert.IsNotNull(statements);
37  Assert.IsNotEmpty(statements);
38  Assert.AreEqual(3, statements.Count());
39  Assert.IsInstanceOf<GrantPrivilegesStatement>(statements.ElementAt(0));
40  Assert.IsInstanceOf<GrantPrivilegesStatement>(statements.ElementAt(1));
41  Assert.IsInstanceOf<GrantPrivilegesStatement>(statements.ElementAt(2));
42 
43  var first = (GrantPrivilegesStatement) statements.ElementAt(0);
44  Assert.AreEqual(Privileges.Select, first.Privilege);
45  Assert.AreEqual("test_user", first.Grantee);
46  Assert.AreEqual("test_table", first.ObjectName.ToString());
47  }
48 
49  [Test]
50  public void ParseGrantRolesToOneUser() {
51  const string sql = "GRANT admin, data_reader TO test_user";
52 
53  var statements = SqlStatement.Parse(sql);
54  Assert.IsNotNull(statements);
55  Assert.IsNotEmpty(statements);
56  Assert.AreEqual(2, statements.Count());
57  Assert.IsInstanceOf<GrantRoleStatement>(statements.ElementAt(0));
58  Assert.IsInstanceOf<GrantRoleStatement>(statements.ElementAt(1));
59 
60  var first = (GrantRoleStatement) statements.ElementAt(0);
61  Assert.AreEqual("admin", first.Role);
62  Assert.AreEqual("test_user", first.UserName);
63  }
64  }
65 }
Represents the foundation class of SQL statements to be executed.
Definition: SqlStatement.cs:32
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
override IQuery CreateQuery(ISession session)
Definition: GrantTests.cs:25
static IEnumerable< SqlStatement > Parse(string sqlSource)
Parses a given string into one of more statements.