/Raven.Tests/Notifications/SecurityOAuth.cs

https://github.com/jalchr/ravendb · C# · 300 lines · 268 code · 27 blank · 5 comment · 0 complexity · d3d8538f3d84653d3c2cd2da1e9115cc MD5 · raw file

  1. // -----------------------------------------------------------------------
  2. // <copyright file="Security.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Net;
  10. using Lucene.Net.Util;
  11. using Raven.Abstractions.Data;
  12. using Raven.Client.Document;
  13. using Raven.Database.Server;
  14. using Raven.Json.Linq;
  15. using Xunit;
  16. namespace Raven.Tests.Notifications
  17. {
  18. public class SecurityOAuth : RavenTest
  19. {
  20. protected override void ModifyConfiguration(Database.Config.InMemoryRavenConfiguration configuration)
  21. {
  22. configuration.AnonymousUserAccessMode = AnonymousUserAccessMode.None;
  23. configuration.PostInit();
  24. }
  25. protected override void CreateDefaultIndexes(Client.IDocumentStore documentStore)
  26. {
  27. }
  28. [Fact]
  29. public void WithOAuthOnSystemDatabase()
  30. {
  31. using (var server = GetNewServer())
  32. {
  33. server.Database.Put("Raven/ApiKeys/test", null, RavenJObject.FromObject(new ApiKeyDefinition
  34. {
  35. Name = "test",
  36. Secret = "test",
  37. Enabled = true,
  38. Databases = new List<DatabaseAccess>
  39. {
  40. new DatabaseAccess {TenantId = "<system>"},
  41. }
  42. }), new RavenJObject(), null);
  43. using (var store = new DocumentStore
  44. {
  45. ApiKey = "test/test",
  46. Url = "http://localhost:8079",
  47. Conventions = { FailoverBehavior = FailoverBehavior.FailImmediately }
  48. }.Initialize())
  49. {
  50. var list = new BlockingCollection<DocumentChangeNotification>();
  51. var taskObservable = store.Changes();
  52. taskObservable.Task.Wait();
  53. var documentSubscription = taskObservable.ForDocument("items/1");
  54. documentSubscription.Task.Wait();
  55. documentSubscription
  56. .Subscribe(list.Add);
  57. using (var session = store.OpenSession())
  58. {
  59. session.Store(new ClientServer.Item(), "items/1");
  60. session.SaveChanges();
  61. }
  62. DocumentChangeNotification changeNotification;
  63. Assert.True(list.TryTake(out changeNotification, TimeSpan.FromSeconds(2)));
  64. Assert.Equal("items/1", changeNotification.Id);
  65. Assert.Equal(changeNotification.Type, DocumentChangeTypes.Put);
  66. }
  67. }
  68. }
  69. [Fact]
  70. public void WithOAuthWrongKeyFails()
  71. {
  72. using (var server = GetNewServer())
  73. {
  74. server.Database.Put("Raven/ApiKeys/test", null, RavenJObject.FromObject(new ApiKeyDefinition
  75. {
  76. Name = "test",
  77. Secret = "test",
  78. Enabled = true,
  79. Databases = new List<DatabaseAccess>
  80. {
  81. new DatabaseAccess {TenantId = "*"},
  82. }
  83. }), new RavenJObject(), null);
  84. using (var store = new DocumentStore
  85. {
  86. ApiKey = "NotRealKeys",
  87. Url = "http://localhost:8079",
  88. Conventions = { FailoverBehavior = FailoverBehavior.FailImmediately }
  89. }.Initialize())
  90. {
  91. Assert.Throws<InvalidOperationException>(() =>
  92. {
  93. using (var session = store.OpenSession())
  94. {
  95. session.Store(new ClientServer.Item(), "items/1");
  96. session.SaveChanges();
  97. }
  98. });
  99. }
  100. }
  101. }
  102. [Fact]
  103. public void WithOAuthOnSpecificDatabase()
  104. {
  105. using (var server = GetNewServer())
  106. {
  107. server.Database.Put("Raven/Databases/OAuthTest", null, RavenJObject.FromObject(new DatabaseDocument
  108. {
  109. Disabled = false,
  110. Id = "Raven/Databases/OAuthTest",
  111. Settings = new IdentityDictionary<string, string>
  112. {
  113. {"Raven/DataDir", "~\\Databases\\OAuthTest"}
  114. }
  115. }), new RavenJObject(), null);
  116. server.Database.Put("Raven/ApiKeys/test", null, RavenJObject.FromObject(new ApiKeyDefinition
  117. {
  118. Name = "test",
  119. Secret = "test",
  120. Enabled = true,
  121. Databases = new List<DatabaseAccess>
  122. {
  123. new DatabaseAccess {TenantId = "OAuthTest"},
  124. }
  125. }), new RavenJObject(), null);
  126. using (var store = new DocumentStore
  127. {
  128. ApiKey = "test/test",
  129. DefaultDatabase = "OAuthTest",
  130. Url = "http://localhost:8079",
  131. Conventions = { FailoverBehavior = FailoverBehavior.FailImmediately }
  132. }.Initialize())
  133. {
  134. var list = new BlockingCollection<DocumentChangeNotification>();
  135. var taskObservable = store.Changes();
  136. taskObservable.Task.Wait();
  137. var documentSubscription = taskObservable.ForDocument("items/1");
  138. documentSubscription.Task.Wait();
  139. documentSubscription
  140. .Subscribe(list.Add);
  141. using (var session = store.OpenSession())
  142. {
  143. session.Store(new ClientServer.Item(), "items/1");
  144. session.SaveChanges();
  145. }
  146. DocumentChangeNotification changeNotification;
  147. Assert.True(list.TryTake(out changeNotification, TimeSpan.FromSeconds(2)));
  148. Assert.Equal("items/1", changeNotification.Id);
  149. Assert.Equal(changeNotification.Type, DocumentChangeTypes.Put);
  150. }
  151. }
  152. }
  153. [Fact]
  154. public void WithOAuthOnSpecificDatabaseWontWorkForAnother()
  155. {
  156. using (var server = GetNewServer())
  157. {
  158. server.Database.Put("Raven/Databases/OAuthTest1", null, RavenJObject.FromObject(new DatabaseDocument
  159. {
  160. Disabled = false,
  161. Id = "Raven/Databases/OAuthTest1",
  162. Settings = new IdentityDictionary<string, string>
  163. {
  164. {"Raven/DataDir", "~\\Databases\\OAuthTest1"}
  165. }
  166. }), new RavenJObject(), null);
  167. server.Database.Put("Raven/Databases/OAuthTest2", null, RavenJObject.FromObject(new DatabaseDocument
  168. {
  169. Disabled = false,
  170. Id = "Raven/Databases/OAuthTest2",
  171. Settings = new IdentityDictionary<string, string>
  172. {
  173. {"Raven/DataDir", "~\\Databases\\OAuthTest2"}
  174. }
  175. }), new RavenJObject(), null);
  176. server.Database.Put("Raven/ApiKeys/test", null, RavenJObject.FromObject(new ApiKeyDefinition
  177. {
  178. Name = "test",
  179. Secret = "test",
  180. Enabled = true,
  181. Databases = new List<DatabaseAccess>
  182. {
  183. new DatabaseAccess {TenantId = "OAuthTest1"},
  184. }
  185. }), new RavenJObject(), null);
  186. using (var store = new DocumentStore
  187. {
  188. ApiKey = "test/test",
  189. DefaultDatabase = "OAuthTest2",
  190. Url = "http://localhost:8079",
  191. Conventions = { FailoverBehavior = FailoverBehavior.FailImmediately }
  192. }.Initialize())
  193. {
  194. Assert.Throws<WebException>(() =>
  195. {
  196. using (var session = store.OpenSession())
  197. {
  198. session.Store(new ClientServer.Item(), "items/1");
  199. session.SaveChanges();
  200. }
  201. });
  202. }
  203. }
  204. }
  205. [Fact]
  206. public void WithOAuthWithStarWorksForAnyDatabaseOtherThenSystem()
  207. {
  208. using (var server = GetNewServer())
  209. {
  210. server.Database.Put("Raven/Databases/OAuthTest", null, RavenJObject.FromObject(new DatabaseDocument
  211. {
  212. Disabled = false,
  213. Id = "Raven/Databases/OAuthTest",
  214. Settings = new IdentityDictionary<string, string>
  215. {
  216. {"Raven/DataDir", "~\\Databases\\OAuthTest"}
  217. }
  218. }), new RavenJObject(), null);
  219. server.Database.Put("Raven/ApiKeys/test", null, RavenJObject.FromObject(new ApiKeyDefinition
  220. {
  221. Name = "test",
  222. Secret = "test",
  223. Enabled = true,
  224. Databases = new List<DatabaseAccess>
  225. {
  226. new DatabaseAccess {TenantId = "*"},
  227. }
  228. }), new RavenJObject(), null);
  229. using (var store = new DocumentStore
  230. {
  231. ApiKey = "test/test",
  232. DefaultDatabase = "OAuthTest",
  233. Url = "http://localhost:8079",
  234. Conventions = { FailoverBehavior = FailoverBehavior.FailImmediately }
  235. }.Initialize())
  236. {
  237. var list = new BlockingCollection<DocumentChangeNotification>();
  238. var taskObservable = store.Changes();
  239. taskObservable.Task.Wait();
  240. var documentSubscription = taskObservable.ForDocument("items/1");
  241. documentSubscription.Task.Wait();
  242. documentSubscription
  243. .Subscribe(list.Add);
  244. using (var session = store.OpenSession())
  245. {
  246. session.Store(new ClientServer.Item(), "items/1");
  247. session.SaveChanges();
  248. }
  249. DocumentChangeNotification changeNotification;
  250. Assert.True(list.TryTake(out changeNotification, TimeSpan.FromSeconds(2)));
  251. Assert.Equal("items/1", changeNotification.Id);
  252. Assert.Equal(changeNotification.Type, DocumentChangeTypes.Put);
  253. }
  254. using (var store = new DocumentStore
  255. {
  256. ApiKey = "test/test",
  257. Url = "http://localhost:8079",
  258. Conventions = { FailoverBehavior = FailoverBehavior.FailImmediately }
  259. }.Initialize())
  260. {
  261. Assert.Throws<WebException>(() =>
  262. {
  263. using (var session = store.OpenSession())
  264. {
  265. session.Store(new ClientServer.Item(), "items/1");
  266. session.SaveChanges();
  267. }
  268. });
  269. }
  270. }
  271. }
  272. }
  273. }