|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using FaunaDB.Client; |
| 5 | +using FaunaDB.Errors; |
| 6 | +using FaunaDB.Types; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +using static FaunaDB.Query.Language; |
| 10 | +using static Test.ClientTest; |
| 11 | + |
| 12 | +namespace Test |
| 13 | +{ |
| 14 | + public class StreamingTest : TestCase |
| 15 | + { |
| 16 | + [OneTimeSetUp] |
| 17 | + public new void SetUp() |
| 18 | + { |
| 19 | + SetUpAsync().Wait(); |
| 20 | + } |
| 21 | + |
| 22 | + async Task SetUpAsync() |
| 23 | + { |
| 24 | + await adminClient.Query(CreateCollection(Obj("name", "streams_test"))); |
| 25 | + } |
| 26 | + |
| 27 | + [Test] |
| 28 | + public async Task TestThatStreamFailsIfTargetDoesNotExist() |
| 29 | + { |
| 30 | + AsyncTestDelegate doc = async () => { await adminClient.Stream(Get(Ref(Collection("streams_test"), "1234"))); }; |
| 31 | + |
| 32 | + var ex = Assert.ThrowsAsync<NotFound>(doc); |
| 33 | + Assert.AreEqual("instance not found: Document not found.", ex.Message); |
| 34 | + AssertErrors(ex, code: "instance not found", description: "Document not found."); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public async Task TestStreamFailsIfIncorrectValuePassedToStreamMethod() |
| 39 | + { |
| 40 | + AsyncTestDelegate doc = async () => { await adminClient.Stream(Collection("streams_test")); }; |
| 41 | + |
| 42 | + var ex = Assert.ThrowsAsync<BadRequest>(doc); |
| 43 | + Assert.AreEqual("invalid argument: Expected a Document Ref or Version, got Collection Ref.", ex.Message); |
| 44 | + AssertErrors(ex, code: "invalid argument", description: "Expected a Document Ref or Version, got Collection Ref."); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public async Task TestStreamFailsIfQueryIsNotReadOnly() |
| 49 | + { |
| 50 | + AsyncTestDelegate doc = async () => { await adminClient.Stream(CreateCollection(Collection("streams_test"))); }; |
| 51 | + |
| 52 | + var ex = Assert.ThrowsAsync<BadRequest>(doc); |
| 53 | + Assert.AreEqual("invalid expression: Write effect in read-only query expression.", ex.Message); |
| 54 | + AssertErrors(ex, code: "invalid expression", description: "Write effect in read-only query expression."); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public async Task TestStreamEventsOnDocumentReferenceWithDocumentFieldByDefault() |
| 59 | + { |
| 60 | + Value createdInstance = await adminClient.Query( |
| 61 | + Create(await RandomCollection(), |
| 62 | + Obj("credentials", |
| 63 | + Obj("password", "abcdefg")))); |
| 64 | + |
| 65 | + var docRef = createdInstance.At("ref"); |
| 66 | + |
| 67 | + var provider = await adminClient.Stream(docRef); |
| 68 | + |
| 69 | + var done = new TaskCompletionSource<object>(); |
| 70 | + |
| 71 | + List<Value> events = new List<Value>(); |
| 72 | + |
| 73 | + var monitor = new StreamingEventMonitor( |
| 74 | + value => |
| 75 | + { |
| 76 | + events.Add(value); |
| 77 | + if (events.Count == 4) |
| 78 | + { |
| 79 | + provider.Complete(); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + provider.RequestData(); |
| 84 | + } |
| 85 | + }, |
| 86 | + ex => { done.SetException(ex); }, |
| 87 | + () => { done.SetResult(null); } |
| 88 | + ); |
| 89 | + |
| 90 | + // subscribe to data provider |
| 91 | + monitor.Subscribe(provider); |
| 92 | + |
| 93 | + // push 3 updates |
| 94 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue1")))); |
| 95 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue2")))); |
| 96 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue3")))); |
| 97 | + |
| 98 | + // blocking until we receive all the events |
| 99 | + await done.Task; |
| 100 | + |
| 101 | + // clear the subscription |
| 102 | + monitor.Unsubscribe(); |
| 103 | + |
| 104 | + Value startEvent = events[0]; |
| 105 | + Assert.AreEqual("start", startEvent.At("type").To<string>().Value); |
| 106 | + |
| 107 | + Value e1 = events[1]; |
| 108 | + Assert.AreEqual("version", e1.At("type").To<string>().Value); |
| 109 | + Assert.AreEqual("testValue1", e1.At("event", "document", "data", "testField").To<string>().Value); |
| 110 | + |
| 111 | + Value e2 = events[2]; |
| 112 | + Assert.AreEqual("version", e1.At("type").To<string>().Value); |
| 113 | + Assert.AreEqual("testValue2", e2.At("event", "document", "data", "testField").To<string>().Value); |
| 114 | + |
| 115 | + Value e3 = events[3]; |
| 116 | + Assert.AreEqual("version", e1.At("type").To<String>().Value); |
| 117 | + Assert.AreEqual("testValue3", e3.At("event", "document", "data", "testField").To<string>().Value); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public async Task TeststreamEventsOnDocumentReferenceWithOptInFields() |
| 122 | + { |
| 123 | + Value createdInstance = await adminClient.Query( |
| 124 | + Create(await RandomCollection(), |
| 125 | + Obj("data", |
| 126 | + Obj("testField", "testValue0")))); |
| 127 | + |
| 128 | + var docRef = createdInstance.At("ref"); |
| 129 | + |
| 130 | + var fields = new List<EventField> { |
| 131 | + EventField.ActionField, |
| 132 | + EventField.DiffField, |
| 133 | + EventField.DocumentField, |
| 134 | + EventField.PrevField |
| 135 | + }; |
| 136 | + |
| 137 | + var provider = await adminClient.Stream(docRef, fields); |
| 138 | + |
| 139 | + var done = new TaskCompletionSource<object>(); |
| 140 | + |
| 141 | + List<Value> events = new List<Value>(); |
| 142 | + |
| 143 | + var monitor = new StreamingEventMonitor( |
| 144 | + value => |
| 145 | + { |
| 146 | + events.Add(value); |
| 147 | + if (events.Count == 4) |
| 148 | + { |
| 149 | + provider.Complete(); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + provider.RequestData(); |
| 154 | + } |
| 155 | + }, |
| 156 | + ex => { done.SetException(ex); }, |
| 157 | + () => { done.SetResult(null); } |
| 158 | + ); |
| 159 | + |
| 160 | + // subscribe to data provider |
| 161 | + monitor.Subscribe(provider); |
| 162 | + |
| 163 | + // push 3 updates |
| 164 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue1")))); |
| 165 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue2")))); |
| 166 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue3")))); |
| 167 | + |
| 168 | + // blocking until we receive all the events |
| 169 | + await done.Task; |
| 170 | + |
| 171 | + // clear the subscription |
| 172 | + monitor.Unsubscribe(); |
| 173 | + |
| 174 | + Value startEvent = events[0]; |
| 175 | + Assert.AreEqual("start", startEvent.At("type").To<string>().Value); |
| 176 | + |
| 177 | + Value e1 = events[1]; |
| 178 | + Assert.AreEqual("version", e1.At("type").To<string>().Value); |
| 179 | + Assert.AreEqual("update", e1.At("event", "action").To<string>().Value); |
| 180 | + Assert.AreEqual( |
| 181 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")), |
| 182 | + ((ObjectV)e1.At("event", "diff", "data")).Value); |
| 183 | + Assert.AreEqual( |
| 184 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")), |
| 185 | + ((ObjectV)e1.At("event", "document", "data")).Value); |
| 186 | + Assert.AreEqual( |
| 187 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue0")), |
| 188 | + ((ObjectV)e1.At("event", "prev", "data")).Value); |
| 189 | + |
| 190 | + Value e2 = events[2]; |
| 191 | + Assert.AreEqual("version", e2.At("type").To<string>().Value); |
| 192 | + Assert.AreEqual("update", e2.At("event", "action").To<string>().Value); |
| 193 | + Assert.AreEqual( |
| 194 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")), |
| 195 | + ((ObjectV)e2.At("event", "diff", "data")).Value); |
| 196 | + Assert.AreEqual( |
| 197 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")), |
| 198 | + ((ObjectV)e2.At("event", "document", "data")).Value); |
| 199 | + Assert.AreEqual( |
| 200 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")), |
| 201 | + ((ObjectV)e2.At("event", "prev", "data")).Value); |
| 202 | + |
| 203 | + Value e3 = events[3]; |
| 204 | + Assert.AreEqual("version", e3.At("type").To<string>().Value); |
| 205 | + Assert.AreEqual("update", e3.At("event", "action").To<string>().Value); |
| 206 | + Assert.AreEqual( |
| 207 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue3")), |
| 208 | + ((ObjectV)e3.At("event", "diff", "data")).Value); |
| 209 | + Assert.AreEqual( |
| 210 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue3")), |
| 211 | + ((ObjectV)e3.At("event", "document", "data")).Value); |
| 212 | + Assert.AreEqual( |
| 213 | + FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")), |
| 214 | + ((ObjectV)e3.At("event", "prev", "data")).Value); |
| 215 | + } |
| 216 | + |
| 217 | + [Test] |
| 218 | + public async Task TestStreamHandlesLossOfAuthorization() |
| 219 | + { |
| 220 | + await adminClient.Query( |
| 221 | + CreateCollection(Obj("name", "streamed-things-auth")) |
| 222 | + ); |
| 223 | + |
| 224 | + Value createdInstance = await adminClient.Query( |
| 225 | + Create(Collection("streamed-things-auth"), |
| 226 | + Obj("credentials", |
| 227 | + Obj("password", "abcdefg")))); |
| 228 | + |
| 229 | + var docRef = createdInstance.At("ref"); |
| 230 | + |
| 231 | + // new key + client |
| 232 | + Value newKey = await adminClient.Query(CreateKey(Obj("role", "server-readonly"))); |
| 233 | + FaunaClient streamingClient = adminClient.NewSessionClient(newKey.At("secret").To<string>().Value); |
| 234 | + |
| 235 | + var provider = await streamingClient.Stream(docRef); |
| 236 | + |
| 237 | + var done = new TaskCompletionSource<object>(); |
| 238 | + |
| 239 | + List<Value> events = new List<Value>(); |
| 240 | + |
| 241 | + var monitor = new StreamingEventMonitor( |
| 242 | + async value => |
| 243 | + { |
| 244 | + if (events.Count == 0) |
| 245 | + { |
| 246 | + try |
| 247 | + { |
| 248 | + // update doc on `start` event |
| 249 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "afterStart")))); |
| 250 | + |
| 251 | + // delete key |
| 252 | + await adminClient.Query(Delete(newKey.At("ref").To<RefV>().Value)); |
| 253 | + |
| 254 | + // push an update to force auth revalidation. |
| 255 | + await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "afterKeyDelete")))); |
| 256 | + } |
| 257 | + catch (Exception ex) |
| 258 | + { |
| 259 | + done.SetException(ex); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + // capture element |
| 264 | + events.Add(value); |
| 265 | + |
| 266 | + // ask for more elements |
| 267 | + provider.RequestData(); |
| 268 | + }, |
| 269 | + ex => { done.SetException(ex); }, |
| 270 | + () => { done.SetResult(null); } |
| 271 | + ); |
| 272 | + |
| 273 | + // subscribe to data provider |
| 274 | + monitor.Subscribe(provider); |
| 275 | + |
| 276 | + // wrapping an asynchronous call |
| 277 | + AsyncTestDelegate res = async () => await done.Task; |
| 278 | + |
| 279 | + // blocking until we get an exception |
| 280 | + var exception = Assert.ThrowsAsync<StreamingException>(res); |
| 281 | + |
| 282 | + // clear the subscription |
| 283 | + monitor.Unsubscribe(); |
| 284 | + |
| 285 | + // validating exception message |
| 286 | + Assert.AreEqual("permission denied: Authorization lost during stream evaluation.", exception.Message); |
| 287 | + AssertErrors(exception, code: "permission denied", description: "Authorization lost during stream evaluation."); |
| 288 | + } |
| 289 | + } |
| 290 | +} |
0 commit comments