public async Task TestConnectionCachingForStoreUserOperation() { using var kernel = new FakeItEasyMockingKernel(); kernel.Rebind <ILogger>().ToConstant(Log.Logger); kernel.Rebind <Configuration>().ToConstant(config); kernel.Rebind <ITeamsUserStore>().To <ImapStore>().InSingletonScope(); kernel.Rebind <ImapConnectionFactory>().ToSelf().InSingletonScope(); var factory = kernel.Get <ImapConnectionFactory>(); var factoryWrap = A.Fake <ImapConnectionFactory>(a => a.Wrapping(factory)); kernel.Rebind <ImapConnectionFactory>().ToConstant(factoryWrap); var imapStore = kernel.Get <ITeamsUserStore>(); var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid"); user.RegisterAlternateDisplayName("Heinrich Ulbricht", DateTime.UtcNow); user.RegisterAlternateEmailAddress("heinrich.ulbricht@localhost", DateTime.UtcNow); var user2 = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid"); user2.RegisterAlternateDisplayName("Heinrich Ulbricht", DateTime.UtcNow); user2.RegisterAlternateEmailAddress("heinrich.ulbricht@localhost", DateTime.UtcNow); await Task.Run(() => { imapStore.PersistUserAsync(fakeContext, user).Wait(); imapStore.PersistUserAsync(fakeContext, user2).Wait(); }); A.CallTo(() => factoryWrap.GetImapConnectionAsync()).MustHaveHappenedOnceExactly(); }
public void TestUseAlternativeName() { var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid", new TeamsUser(), ProcessedTeamsUser.TeamsUserState.MissingFromTenant); Assert.IsFalse(user.HasDisplayName); user.RegisterAlternateDisplayName("Name 1", new DateTime(1000)); Assert.IsTrue(user.HasDisplayName); Assert.AreEqual("Name 1", user.DisplayName); }
public void TestIgnoreMalformedMri() { var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid", new TeamsUser(), ProcessedTeamsUser.TeamsUserState.MissingFromTenant); Assert.IsFalse(user.HasDisplayName); user.RegisterAlternateDisplayName("This is expected", new DateTime(100)); user.RegisterAlternateDisplayName("orgid:00000000-0000-beef-0000-000000000000", new DateTime(500)); Assert.IsTrue(user.HasDisplayName); Assert.AreEqual("This is expected", user.DisplayName); }
public void DuplicateDoesNotDestroyOrder() { var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid", new TeamsUser(), ProcessedTeamsUser.TeamsUserState.MissingFromTenant); Assert.IsFalse(user.HasDisplayName); user.RegisterAlternateDisplayName("Name", new DateTime(1000)); user.RegisterAlternateDisplayName("Name", new DateTime(200)); user.RegisterAlternateDisplayName("Name 500", new DateTime(500)); Assert.IsTrue(user.HasDisplayName); Assert.AreEqual("Name", user.DisplayName); }
public void TestDuplicateNameNotRecordedTwice() { var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid", new TeamsUser(), ProcessedTeamsUser.TeamsUserState.MissingFromTenant); Assert.IsFalse(user.HasDisplayName); user.RegisterAlternateDisplayName("Name Picard", new DateTime(200)); user.RegisterAlternateDisplayName("Name Picard", new DateTime(1000)); var json = JsonConvert.SerializeObject(user); Assert.IsTrue(json.IndexOf("Name Picard") == json.LastIndexOf("Name Picard"), "Found name more than once; that must not happen"); user.RegisterAlternateDisplayName("Name Picard", new DateTime(100)); json = JsonConvert.SerializeObject(user); Assert.IsTrue(json.IndexOf("Name Picard") == json.LastIndexOf("Name Picard"), "Found name more than once; that must not happen"); }
public void TestProcessedTeamsUserSerialization() { var user = new ProcessedTeamsUser(new TeamsDataContext((TeamsParticipant)"8:orgid:00000000-0000-beef-0000-000000000000", new ProcessedTenant(new TeamsInternal.TeamsInternalApi.api.mt.emea.beta.users.Tenant() { tenantName = "Test Tenant" }, DateTime.Now)), (TeamsParticipant)"8:orgid:00000000-0000-beef-0000-000000000000") { State = ProcessedTeamsUser.TeamsUserState.MissingFromTenant }; var dt = DateTime.UtcNow; user.RegisterAlternateDisplayName("alternate display name", dt); var json = JsonConvert.SerializeObject(user); var deserializedUser = JsonConvert.DeserializeObject <ProcessedTeamsUser>(json); Assert.AreEqual("alternate display name", deserializedUser.DisplayName); }
public async Task TestWriteAndReadUser() { using var kernel = new FakeItEasyMockingKernel(); kernel.Rebind <ILogger>().ToConstant(Log.Logger); kernel.Rebind <Configuration>().ToConstant(config); kernel.Rebind <ITeamsUserStore>().To <ImapStore>().InSingletonScope(); var imapStore = kernel.Get <ITeamsUserStore>(); var userName = new Random().Next().ToString(); var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid"); user.RegisterAlternateDisplayName(userName, DateTime.UtcNow); user.RegisterAlternateEmailAddress("heinrich.ulbricht@localhost", DateTime.UtcNow); await imapStore.PersistUserAsync(fakeContext, user); var users = await imapStore.RetrieveUsersAsync(fakeContext); Assert.AreEqual(1, users.Where(u => u.DisplayName == userName).Count(), "Cannot find previously saved user"); }
public void TestComplexValue() { var store = new EmailBackedKeyValueStore(null, "store key", "message subject"); var testObject = new ProcessedTeamsUser(new TeamsDataContext((TeamsParticipant)"8:orgid:00000000-0000-beef-0000-000000000000", new ProcessedTenant(new TeamsInternal.TeamsInternalApi.api.mt.emea.beta.users.Tenant() { tenantName = "Test Tenant" }, DateTime.Now)), (TeamsParticipant)"8:orgid:00000000-0000-beef-0000-000000000000") { State = ProcessedTeamsUser.TeamsUserState.MissingFromTenant }; store.Set("my key", testObject); var readValue = store.GetOrCreateEmpty <ProcessedTeamsUser>("my key"); Assert.AreEqual(ProcessedTeamsUser.TeamsUserState.MissingFromTenant, readValue.AsObject?.State); Assert.AreEqual("8:orgid:00000000-0000-beef-0000-000000000000", readValue.AsObject?.DataContext.MainUserId.Mri); Assert.AreEqual("00000000-0000-beef-0000-000000000000", readValue.AsObject?.DataContext.MainUserId.Id); Assert.AreEqual(ParticipantKind.User, readValue.AsObject?.DataContext.MainUserId.Kind); Assert.AreEqual("Test Tenant", readValue.AsObject?.DataContext.Tenant.TenantName); }
public void TestTextAndHtmlContent() { var store = new EmailBackedKeyValueStore(null, "store key", "message subject"); var testObject = new ProcessedTeamsUser(new TeamsDataContext((TeamsParticipant)"8:orgid:00000000-0000-beef-0000-000000000000", new ProcessedTenant(new TeamsInternal.TeamsInternalApi.api.mt.emea.beta.users.Tenant() { tenantName = "Test Tenant" }, DateTime.Now)), (TeamsParticipant)"8:orgid:00000000-0000-beef-0000-000000000000") { State = ProcessedTeamsUser.TeamsUserState.MissingFromTenant }; store.Set("my key", testObject); store.SetTextContent("i am a text"); store.SetHtmlContent("i am HTML"); Assert.AreEqual("i am a text", store.GetTextContent()); Assert.AreEqual("i am HTML", store.GetHtmlContent()); var attachments = new MimeKit.AttachmentCollection(); using (var stream = new MemoryStream()) { stream.Write(Encoding.UTF8.GetBytes("i am an attachment")); stream.Position = 0; attachments.Add("test.txt", stream); } store.AddAttachments(attachments); Assert.AreEqual(1, store.MessageAndId.Message?.Attachments.Count()); using (var debugStream = new MemoryStream()) { store.MessageAndId.Message?.WriteTo(debugStream); var s = Encoding.ASCII.GetString(debugStream.ToArray()); } var readValue = store.GetOrCreateEmpty <ProcessedTeamsUser>("my key"); Assert.AreEqual("8:orgid:00000000-0000-beef-0000-000000000000", readValue.AsObject?.DataContext.MainUserId.Mri); }
public async Task TestWriteAndReadManyUsers() { using var kernel = new FakeItEasyMockingKernel(); kernel.Rebind <ILogger>().ToConstant(Log.Logger); kernel.Rebind <Configuration>().ToConstant(config); kernel.Rebind <ITeamsUserStore>().To <ImapStore>().InSingletonScope(); var imapStore = kernel.Get <ITeamsUserStore>(); var sw = new Stopwatch(); sw.Start(); for (var i = 0; i < 10; i++) { var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)i.ToString()); user.RegisterAlternateDisplayName("Massentest", DateTime.UtcNow); user.RegisterAlternateEmailAddress("heinrich.ulbricht@localhost", DateTime.UtcNow); await imapStore.PersistUserAsync(fakeContext, user); } var users = await imapStore.RetrieveUsersAsync(fakeContext); sw.Stop(); Assert.IsTrue(sw.ElapsedMilliseconds < 10000, "Needing quite a lot of time to store users"); }
protected async Task ExtractSendersReceiversAndSubject(string chatId) { if (ctx == null || !ctx.HasValue) { return; } var nonNullContext = ctx.Value; var messageFromTeams = false; var messageFromMe = false; if (Internal_FromContactUrl?.EndsWith(chatId, StringComparison.InvariantCultureIgnoreCase) ?? false) { messageFromTeams = true; } #pragma warning disable CS8604 // Possible null reference argument. if (Internal_FromContactUrl?.ToString().EndsWith(nonNullContext.Tenant.UserId, StringComparison.InvariantCultureIgnoreCase) ?? false) #pragma warning restore CS8604 // Possible null reference argument. { messageFromMe = true; } // determine senders if (messageFromTeams) { var teamsChatUser = new ProcessedTeamsUser(nonNullContext, (TeamsParticipant)chatId); teamsChatUser.RegisterAlternateDisplayName(Constants.MicrosoftTeamsChatSenderName, OriginalArrivalTime); teamsChatUser.RegisterAlternateEmailAddress(Constants.MicrosoftTeamsChatSenderEmailAddress, OriginalArrivalTime); From.Add(teamsChatUser); } else { var userId = (TeamsParticipant)Internal_FromContactUrl; if (userId.IsValid) { await teamsUserRegistry.RegisterDisplayNameForUserIdAsync(nonNullContext, userId, Internal_DisplayName, OriginalArrivalTime); var user = await teamsUserRegistry.GetUserByIdOrDummyAsync(nonNullContext, userId); if (user.HasDisplayName && user.HasEmailAddress) { From.Add(user); } } else { if (From.Count == 0) { // no valid user id? phew... var dummyUser = new ProcessedTeamsUser(nonNullContext, userId); dummyUser.RegisterAlternateDisplayName(Internal_DisplayName, OriginalArrivalTime); From.Add(dummyUser); } } } // determine receivers if (Internal_Mentions?.Count > 0) { foreach (var mention in Internal_Mentions) { var userId = (TeamsParticipant)mention.mri; var user = await teamsUserRegistry.GetUserByIdAsync(nonNullContext, userId, true); if (user != null && user.HasDisplayName && user.HasEmailAddress) { To.Add(user); } else { var toUser = new ProcessedTeamsUser(nonNullContext, userId); toUser.RegisterAlternateDisplayName(mention.displayName, OriginalArrivalTime); toUser.RegisterAlternateEmailAddress(mention.IsChannelMention ? Constants.ChannelMentionEmailAddress : Constants.UnknownUserEmailAddress, OriginalArrivalTime); To.Add(toUser); } } } else { var toUser = new ProcessedTeamsUser(nonNullContext, TeamsParticipant.Null); toUser.RegisterAlternateDisplayName(Constants.UnknownReceiversDisplayName, OriginalArrivalTime); toUser.RegisterAlternateEmailAddress(Constants.UnknownUserEmailAddress, OriginalArrivalTime); To.Add(toUser); } // determine subject if (!string.IsNullOrWhiteSpace(Internal_Subject)) { MessageSubject = Internal_Subject; } else { string displayName = Internal_DisplayName ?? ""; if (displayName == null && (Messagetype != MessageType.Text || Messagetype != MessageType.RichText_Html)) { displayName = "Microsoft Teams"; } await teamsUserRegistry.RegisterDisplayNameForUserIdAsync(nonNullContext, (TeamsParticipant)Internal_FromContactUrl, displayName, OriginalArrivalTime); displayName = await teamsUserRegistry.ReplaceUserIdsWithDisplayNamesAsync(nonNullContext, displayName); var messagePreview = "Message"; if (Messagetype == MessageType.RichText_Html) { // not sure if needed string html = string.Format("<html><head></head><body>{0}</body></html>", Internal_Content); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); var body = doc.DocumentNode.SelectSingleNode("//body"); messagePreview = body.InnerText ?? ""; } else if (Messagetype == MessageType.Text) { messagePreview = Internal_Content ?? ""; } messagePreview = HttpUtility.HtmlDecode(messagePreview).Trim(); // need to replace etc. if (messagePreview.Length > 100) { messagePreview = messagePreview.Truncate(100) + "..."; } else if (messagePreview.Length == 0) { messagePreview = $"Message"; } var truncatedDisplayName = displayName.Truncate(10)?.Trim() ?? ""; MessageSubject = $"[{(messageFromMe ? "ME" : truncatedDisplayName)}] {messagePreview}"; } }