Exemple #1
0
        public void TestDuplicateGivesNoError()
        {
            var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid", new TeamsUser(), ProcessedTeamsUser.TeamsUserState.MissingFromTenant);

            Assert.IsFalse(user.HasDisplayName);
            user.RegisterAlternateDisplayName("Name 200", new DateTime(200));
            user.RegisterAlternateDisplayName("Name 200", new DateTime(200));
            Assert.IsTrue(user.HasDisplayName);
            Assert.AreEqual("Name 200", user.DisplayName);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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");
        }
Exemple #4
0
        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();
        }
Exemple #5
0
        public void TestIgnoreAlternativeName()
        {
            var user = new ProcessedTeamsUser(fakeContext, (TeamsParticipant)"test.fakeuserid", new TeamsUser()
            {
                displayName = "Name 1"
            }, ProcessedTeamsUser.TeamsUserState.MissingFromTenant);

            Assert.IsTrue(user.HasDisplayName);
            Assert.AreEqual("Name 1", user.DisplayName);
            user.RegisterAlternateDisplayName("Name 2", new DateTime(1000));
            Assert.IsTrue(user.HasDisplayName);
            Assert.AreEqual("Name 1", user.DisplayName);
        }
        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);
        }
Exemple #7
0
        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");
        }
Exemple #8
0
        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");
        }
Exemple #9
0
        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 &nbsp; 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}";
            }
        }