|
1 |
| -using Keen.Core; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
2 | 4 | using Keen.AccessKey;
|
| 5 | +using Keen.Core; |
3 | 6 | using Keen.Query;
|
4 | 7 | using Newtonsoft.Json.Linq;
|
5 | 8 | using NUnit.Framework;
|
6 |
| -using System; |
7 |
| -using System.Collections.Generic; |
8 |
| -using System.Linq; |
9 |
| -using System.Text; |
10 |
| -using System.Threading.Tasks; |
| 9 | + |
11 | 10 |
|
12 | 11 | namespace Keen.Test
|
13 | 12 | {
|
14 | 13 | [TestFixture]
|
15 | 14 | class AccessKeyTests : TestBase
|
16 | 15 | {
|
| 16 | + private IProjectSettings _settings = null; |
| 17 | + private KeenClient _client = null; |
| 18 | + |
| 19 | + [SetUp] |
| 20 | + public void AccessKeyTestsSetup() |
| 21 | + { |
| 22 | + _settings = new ProjectSettingsProvider(projectId: "X", |
| 23 | + masterKey: SettingsEnv.MasterKey); |
| 24 | + _client = new KeenClient(_settings); |
| 25 | + } |
| 26 | + |
17 | 27 | [Test]
|
18 | 28 | public void CreateAccessKey_Success()
|
19 | 29 | {
|
20 |
| - var settings = new ProjectSettingsProvider(projectId: "X", masterKey: SettingsEnv.MasterKey); // Replace X with respective value |
21 |
| - var client = new KeenClient(settings); |
22 |
| - |
| 30 | + // TODO : Replace AccessKeysMock with Moq as per PR feedback. |
23 | 31 | if (UseMocks)
|
24 |
| - client.AccessKeys = new AccessKeysMock(settings, |
25 |
| - createAccessKey: new Func<AccessKey.AccessKey, IProjectSettings, JObject>((e, p) => |
| 32 | + _client.AccessKeys = new AccessKeysMock(_settings, |
| 33 | + createAccessKey: new Func<AccessKeyDefinition, IProjectSettings, JObject>((e, p) => |
26 | 34 | {
|
27 |
| - Assert.True(p == settings, "Incorrect Settings"); |
| 35 | + Assert.True(p == _settings, "Incorrect Settings"); |
28 | 36 | Assert.NotNull(e.Name, "Expected a name for the newly created Key");
|
29 | 37 | Assert.NotNull(e.Permitted, "Expected a list of high level actions this key can perform");
|
30 | 38 | Assert.NotNull(e.Options, "Expected an object containing more details about the key’s permitted and restricted functionality");
|
31 |
| - if ((p == settings) && (e.Name == "TestAccessKey") && (e.IsActive) && e.Permitted.First() == "queries" && e.Options.CachedQueries.Allowed.First() == "my_cached_query") |
| 39 | + if ((p == _settings) && (e.Name == "TestAccessKey") && (e.IsActive) && e.Permitted.First() == "queries" && e.Options.CachedQueries.Allowed.First() == "my_cached_query") |
32 | 40 | return new JObject();
|
33 | 41 | else
|
34 | 42 | throw new Exception("Unexpected value");
|
35 | 43 | }));
|
36 | 44 |
|
37 |
| - HashSet<string> permissions = new HashSet<string>() { "queries" }; |
38 |
| - List<QueryFilter> qFilters = new List<QueryFilter>() { new QueryFilter("customer.id", QueryFilter.FilterOperator.Equals(), "asdf12345z") }; |
39 |
| - CachedQueries cachedQueries = new CachedQueries(); |
40 |
| - cachedQueries.Allowed = new HashSet<string>() { "my_cached_query" }; |
41 |
| - Options options = new Options() |
| 45 | + var permitted = new HashSet<string>() { "queries" }; |
| 46 | + |
| 47 | + var filters = new List<QueryFilter>() |
42 | 48 | {
|
43 |
| - Queries = new AccessKey.Queries { Filters = qFilters }, |
| 49 | + new QueryFilter("customer.id", QueryFilter.FilterOperator.Equals(), "asdf12345z") |
| 50 | + }; |
| 51 | + |
| 52 | + var cachedQueries = new CachedQueries |
| 53 | + { |
| 54 | + Allowed = new HashSet<string>() { "my_cached_query" } |
| 55 | + }; |
| 56 | + |
| 57 | + var options = new Options() |
| 58 | + { |
| 59 | + Queries = new AccessKey.Queries { Filters = filters }, |
44 | 60 | CachedQueries = cachedQueries
|
45 | 61 | };
|
46 | 62 |
|
47 |
| - Assert.DoesNotThrow(() => client.CreateAccessKey(new AccessKey.AccessKey { Name = "TestAccessKey", IsActive = true, Options = options, Permitted = permissions })); |
48 |
| - } |
| 63 | + var accessKey = new AccessKeyDefinition |
| 64 | + { |
| 65 | + Name = "TestAccessKey", |
| 66 | + IsActive = true, |
| 67 | + Permitted = permitted, |
| 68 | + Options = options |
| 69 | + }; |
49 | 70 |
|
| 71 | + Assert.DoesNotThrow(() => _client.CreateAccessKey(accessKey)); |
| 72 | + } |
50 | 73 |
|
51 | 74 | [Test]
|
52 | 75 | public void CreateAccessKey_With_All_Properties_Given_As_Null_Success()
|
53 | 76 | {
|
54 |
| - var settings = new ProjectSettingsProvider(projectId: "X", masterKey: SettingsEnv.MasterKey); // Replace X with respective value |
55 |
| - var client = new KeenClient(settings); |
56 |
| - |
| 77 | + // TODO : Replace AccessKeysMock with Moq as per PR feedback. |
57 | 78 | if (UseMocks)
|
58 |
| - client.AccessKeys = new AccessKeysMock(settings, |
59 |
| - createAccessKey: new Func<AccessKey.AccessKey, IProjectSettings, JObject>((e, p) => |
| 79 | + _client.AccessKeys = new AccessKeysMock(_settings, |
| 80 | + createAccessKey: new Func<AccessKeyDefinition, IProjectSettings, JObject>((e, p) => |
60 | 81 | {
|
61 |
| - Assert.True(p == settings, "Incorrect Settings"); |
| 82 | + Assert.True(p == _settings, "Incorrect Settings"); |
62 | 83 | Assert.NotNull(e.Name, "Expected a name for the newly created Key");
|
63 | 84 | Assert.NotNull(e.Permitted, "Expected a list of high level actions this key can perform");
|
64 | 85 | Assert.NotNull(e.Options, "Expected an object containing more details about the key’s permitted and restricted functionality");
|
65 |
| - if ((p == settings) && (e.Name == "TestAccessKey") && (e.IsActive) && e.Permitted.First() == "queries" && e.Options.CachedQueries.Allowed.First() == "my_cached_query") |
| 86 | + if ((p == _settings) && (e.Name == "TestAccessKey") && (e.IsActive) && e.Permitted.First() == "queries") |
66 | 87 | return new JObject();
|
67 | 88 | else
|
68 | 89 | throw new Exception("Unexpected value");
|
69 | 90 | }));
|
70 | 91 |
|
71 |
| - HashSet<string> permissions = new HashSet<string>() { "queries" }; |
72 |
| - List<QueryFilter> qFilters = new List<QueryFilter>() { new QueryFilter("customer.id", QueryFilter.FilterOperator.Equals(), "asdf12345z") }; |
73 |
| - CachedQueries cachedQueries = new CachedQueries() { Allowed = null, Blocked = null }; |
74 |
| - SavedQueries savedQuaries = new SavedQueries() { Allowed = null, Blocked = null, Filters = null }; |
75 |
| - Datasets datasets = new Datasets() { Allowed = null, Blocked = null, Operations = null }; |
76 |
| - Writes writes = new Writes() { Autofill = null }; |
77 |
| - cachedQueries.Allowed = new HashSet<string>() { "my_cached_query" }; |
78 |
| - Options options = new Options() |
| 92 | + var permitted = new HashSet<string>() { "queries" }; |
| 93 | + |
| 94 | + // TODO : Can't null just be the default when we construct these? We should look at |
| 95 | + // some factories or a builder mechanism to configure these correctly, or at least add |
| 96 | + // more validation code that will throw before sending with reasons for why it's |
| 97 | + // malformed. For example, why set a SavedQueries instance if "saved_queries" isn't |
| 98 | + // permitted? We could help devs consuming the SDK catch that early. |
| 99 | + |
| 100 | + var cachedQueries = new CachedQueries() { Allowed = null, Blocked = null }; |
| 101 | + |
| 102 | + var savedQuaries = new SavedQueries() |
79 | 103 | {
|
80 |
| - Queries = new AccessKey.Queries { Filters = qFilters }, |
| 104 | + Allowed = null, |
| 105 | + Blocked = null, |
| 106 | + Filters = null |
| 107 | + }; |
| 108 | + |
| 109 | + var datasets = new Datasets() { Allowed = null, Blocked = null, Operations = null }; |
| 110 | + var writes = new Writes() { Autofill = null }; |
| 111 | + |
| 112 | + var options = new Options() |
| 113 | + { |
| 114 | + Queries = new AccessKey.Queries { Filters = null }, |
81 | 115 | CachedQueries = cachedQueries,
|
82 | 116 | SavedQueries = savedQuaries,
|
83 | 117 | Datasets = datasets,
|
84 | 118 | Writes = writes
|
85 | 119 | };
|
86 | 120 |
|
87 |
| - Assert.DoesNotThrow(() => client.CreateAccessKey(new AccessKey.AccessKey { Name = "TestAccessKey", IsActive = true, Options = options, Permitted = permissions })); |
88 |
| - } |
| 121 | + // TODO : We need to more carefully test out what is required and what isn't. For |
| 122 | + // example, I thought the 'options' member could be omitted. |
89 | 123 |
|
| 124 | + var accessKey = new AccessKeyDefinition |
| 125 | + { |
| 126 | + Name = "TestAccessKey", |
| 127 | + IsActive = true, |
| 128 | + Permitted = permitted, |
| 129 | + Options = options |
| 130 | + }; |
| 131 | + |
| 132 | + Assert.DoesNotThrow(() => _client.CreateAccessKey(accessKey)); |
| 133 | + } |
90 | 134 | }
|
91 | 135 | }
|
0 commit comments