public async Task Should_enrich_with_user_and_cache() { var actor = RefToken.Client("me"); var user = A.Dummy <IUser>(); A.CallTo(() => userResolver.FindByIdAsync(actor.Identifier)) .Returns(user); var event1 = Envelope.Create <AppEvent>(new ContentCreated { Actor = actor }); var event2 = Envelope.Create <AppEvent>(new ContentCreated { Actor = actor }); var enrichedEvent1 = new EnrichedContentEvent(); var enrichedEvent2 = new EnrichedContentEvent(); await sut.EnrichAsync(enrichedEvent1, event1); await sut.EnrichAsync(enrichedEvent2, event2); Assert.Equal(user, enrichedEvent1.User); Assert.Equal(user, enrichedEvent2.User); A.CallTo(() => userResolver.FindByIdAsync(A <string> ._)) .MustHaveHappenedOnceExactly(); }
public async IAsyncEnumerable <EnrichedEvent> CreateSnapshotEventsAsync(RuleContext context, [EnumeratorCancellation] CancellationToken ct = default) { var trigger = (ContentChangedTriggerV2)context.Rule.Trigger; var schemaIds = trigger.Schemas?.Count > 0 ? trigger.Schemas.Select(x => x.SchemaId).Distinct().ToHashSet() : null; await foreach (var content in contentRepository.StreamAll(context.AppId.Id, schemaIds, ct)) { var result = new EnrichedContentEvent { Type = EnrichedContentEventType.Created }; SimpleMapper.Map(content, result); result.Actor = content.LastModifiedBy; result.Name = $"ContentQueried({content.SchemaId.Name.ToPascalCase()})"; yield return(result); } }
public async Task <EnrichedEvent> EnrichAsync(Envelope <AppEvent> @event) { Guard.NotNull(@event, nameof(@event)); if (@event.Payload is ContentEvent contentEvent) { var result = new EnrichedContentEvent(); await Task.WhenAll( EnrichContentAsync(result, contentEvent, @event), EnrichDefaultAsync(result, @event)); return(result); } if (@event.Payload is AssetEvent assetEvent) { var result = new EnrichedAssetEvent(); await Task.WhenAll( EnrichAssetAsync(result, assetEvent, @event), EnrichDefaultAsync(result, @event)); return(result); } return(null); }
private (TemplateVars, IAssetEntity) SetupAssetVars(int fileSize = 100) { var assetId = DomainId.NewGuid(); var asset = CreateAsset(assetId, 1, fileSize); var @event = new EnrichedContentEvent { Data = new ContentData() .AddField("assets", new ContentFieldData() .AddInvariant(JsonValue.Array(assetId))), AppId = appId }; A.CallTo(() => assetQuery.FindAsync(A <Context> ._, assetId, EtagVersion.Any, A <CancellationToken> ._)) .Returns(asset); SetupText(@event.Id, Encoding.UTF8.GetBytes("Hello Asset")); var vars = new TemplateVars { ["event"] = @event }; return(vars, asset); }
private (TemplateVars, IAssetEntity[]) SetupAssetsVars(int fileSize = 100) { var assetId1 = DomainId.NewGuid(); var asset1 = CreateAsset(assetId1, 1, fileSize); var assetId2 = DomainId.NewGuid(); var asset2 = CreateAsset(assetId2, 2, fileSize); var @event = new EnrichedContentEvent { Data = new ContentData() .AddField("assets", new ContentFieldData() .AddInvariant(JsonValue.Array(assetId1, assetId2))), AppId = appId }; A.CallTo(() => assetQuery.FindAsync(A <Context> ._, assetId1, EtagVersion.Any, A <CancellationToken> ._)) .Returns(asset1); A.CallTo(() => assetQuery.FindAsync(A <Context> ._, assetId2, EtagVersion.Any, A <CancellationToken> ._)) .Returns(asset2); var vars = new TemplateVars { ["event"] = @event }; return(vars, new[] { asset1, asset2 }); }
public void Should_return_null_when_asset_content_url_not_found(string script) { var @event = new EnrichedContentEvent(); var result = sut.Format(script, @event); Assert.Equal("Download at null", result); }
public void Should_return_null_if_user_is_not_found(string script) { var @event = new EnrichedContentEvent(); var result = sut.Format(script, @event); Assert.Equal("From null (null, null)", result); }
public void Should_format_email_and_display_name_from_user() { var @event = new EnrichedContentEvent { User = user, Actor = new RefToken(RefTokenType.Subject, "123") }; var result = sut.Format("From $USER_NAME ($USER_EMAIL)", @event); Assert.Equal("From me ([email protected])", result); }
public void Should_create_payload() { var @event = new EnrichedContentEvent { AppId = appId }; var result = sut.ToPayload(@event); Assert.NotNull(result); }
public void Should_evaluate_script_if_ends_with_whitespace() { var @event = new EnrichedContentEvent { Type = EnrichedContentEventType.Created }; var result = sut.Format("Script(`${event.type}`) ", @event); Assert.Equal("Created", result); }
public void Should_format_email_and_display_name_from_user(string script) { var @event = new EnrichedContentEvent { User = user }; var result = sut.Format(script, @event); Assert.Equal("From me ([email protected], 123)", result); }
public void Should_replace_schema_information_from_event() { var @event = new EnrichedContentEvent { SchemaId = schemaId }; var result = sut.Format("Name $SCHEMA_NAME has id $SCHEMA_ID", @event); Assert.Equal($"Name my-schema has id {schemaId.Id}", result); }
public void Should_replace_app_information_from_event() { var @event = new EnrichedContentEvent { AppId = appId }; var result = sut.Format("Name $APP_NAME has id $APP_ID", @event); Assert.Equal($"Name my-app has id {appId.Id}", result); }
public void Should_format_email_and_display_name_from_client() { var @event = new EnrichedContentEvent { Actor = new RefToken(RefTokenType.Client, "android") }; var result = sut.Format("From $USER_NAME ($USER_EMAIL)", @event); Assert.Equal("From client:android (client:android)", result); }
public void Should_return_undefined_if_user_is_not_found() { var @event = new EnrichedContentEvent { Actor = new RefToken(RefTokenType.Subject, "123") }; var result = sut.Format("From $USER_NAME ($USER_EMAIL)", @event); Assert.Equal("From UNDEFINED (UNDEFINED)", result); }
public void Should_replace_schema_information_from_event(string script) { var @event = new EnrichedContentEvent { SchemaId = schemaId }; var result = sut.Format(script, @event); Assert.Equal($"Name my-schema has id {schemaId.Id}", result); }
public void Should_replace_timestamp_information_from_event(string script) { var @event = new EnrichedContentEvent { Timestamp = now }; var result = sut.Format(script, @event); Assert.Equal($"Date: {now:yyyy-MM-dd}, Full: {now:yyyy-MM-dd-hh-mm-ss}", result); }
public void Should_create_envelope_data_from_event() { var @event = new EnrichedContentEvent { AppId = appId, Name = "MyEventName" }; var result = sut.ToEnvelope(@event); Assert.Contains("MyEventName", result); }
public void Should_format_json_with_special_characters() { var @event = new EnrichedContentEvent { Actor = new RefToken(RefTokenType.Client, "mobile\"android") }; var result = sut.Format("Script(JSON.stringify({ actor: event.actor.toString() }))", @event); Assert.Equal("{\"actor\":\"client:mobile\\\"android\"}", result); }
public void Should_format_actor(string script) { var @event = new EnrichedContentEvent { Actor = new RefToken(RefTokenType.Client, "android") }; var result = sut.Format(script, @event); Assert.Equal("From client:android", result); }
public void Should_replace_app_information_from_event(string script) { var @event = new EnrichedContentEvent { AppId = appId }; var result = sut.Format(script, @event); Assert.Equal($"Name my-app has id {appId.Id}", result); }
public void Should_format_content_actions_when_found(string script) { var @event = new EnrichedContentEvent { Type = EnrichedContentEventType.Created }; var result = sut.Format(script, @event); Assert.Equal("Created", result); }
public void Should_format_content_status_when_found(string script) { var @event = new EnrichedContentEvent { Status = Status.Published }; var result = sut.Format(script, @event); Assert.Equal("Published", result); }
public void Should_replace_content_url_from_event(string script) { var @event = new EnrichedContentEvent { AppId = appId, Id = contentId, SchemaId = schemaId }; var result = sut.Format(script, @event); Assert.Equal("Go to content-url", result); }
public void Should_format_email_and_display_name_from_client(string script) { var @event = new EnrichedContentEvent { User = new ClientUser(new RefToken(RefTokenType.Client, "android")) }; var result = sut.Format(script, @event); Assert.Equal("From client:android (client:android, android)", result); }
public void Should_replace_timestamp_information_from_event(string script) { var now = SystemClock.Instance.GetCurrentInstant(); var envelope = new EnrichedContentEvent { Timestamp = now }; var result = sut.Format(script, envelope); Assert.Equal($"Date: {now:yyyy-MM-dd}, Full: {now:yyyy-MM-dd-hh-mm-ss}", result); }
public void Should_replace_timestamp_information_from_event() { var now = DateTime.UtcNow; var envelope = new EnrichedContentEvent { Timestamp = Instant.FromDateTimeUtc(now) }; var result = sut.Format("Date: $TIMESTAMP_DATE, Full: $TIMESTAMP_DATETIME", envelope); Assert.Equal($"Date: {now:yyyy-MM-dd}, Full: {now:yyyy-MM-dd-hh-mm-ss}", result); }
public void Should_trigger_check_if_handling_all_events() { TestForTrigger(handleAll: true, schemaId: schemaMatch, condition: null, action: ctx => { var @event = new EnrichedContentEvent { SchemaId = schemaMatch }; var result = sut.Trigger(@event, ctx); Assert.True(result); }); }
public void Should_not_trigger_check_if_condition_does_not_match() { TestForTrigger(handleAll: false, schemaId: schemaMatch, condition: "false", action: ctx => { var @event = new EnrichedContentEvent { SchemaId = schemaMatch }; var result = sut.Trigger(@event, ctx); Assert.False(result); }); }
public void Should_trigger_check_if_condition_is_empty() { TestForTrigger(handleAll: false, schemaId: schemaMatch, condition: string.Empty, action: ctx => { var @event = new EnrichedContentEvent { SchemaId = schemaMatch }; var result = sut.Trigger(@event, ctx); Assert.True(result); }); }