Exemplo n.º 1
0
        public async Task TestCreateMessage()
        {
            var serviceDescriptors = new ServiceCollection();

            serviceDescriptors.AddSingleton <PacketService>();
            serviceDescriptors.AddTestDatabaseContext($"{nameof(MessageInjectionServiceTests)}_{nameof(TestCreateMessage)}");
            serviceDescriptors.AddScoped <MessageInjectionService>();
            await using var serviceProvider = serviceDescriptors.BuildServiceProvider();

            using IServiceScope scope = serviceProvider.CreateScope();
            var packets  = scope.ServiceProvider.GetRequiredService <PacketService>();
            var database = scope.ServiceProvider.GetRequiredService <DatabaseContext>();
            var injector = scope.ServiceProvider.GetRequiredService <MessageInjectionService>();

            var packet = packets.New <P14MailAddress>();

            packet.MessageFlags = MessageFlags.Unencrypted;
            packet.MailAddress  = "*****@*****.**";

            long channelId = SkynetRandom.Id();
            long accountId = SkynetRandom.Id();

            await injector.CreateMessage(packet, channelId, accountId).ConfigureAwait(false);

            Message message = await database.Messages.SingleAsync().ConfigureAwait(false);

            Assert.AreEqual(channelId, message.ChannelId);
            Assert.AreEqual(accountId, message.SenderId);
            Assert.AreEqual(MessageFlags.Unencrypted, message.MessageFlags);
        }
Exemplo n.º 2
0
        public async Task <Channel> AddChannel(Channel channel, params ChannelMember[] members)
        {
            bool saved = false;

            do
            {
                try
                {
                    long id = SkynetRandom.Id();
                    channel.ChannelId = id;
                    Channels.Add(channel);

                    foreach (ChannelMember member in members)
                    {
                        member.ChannelId = id;
                    }

                    ChannelMembers.AddRange(members);

                    await SaveChangesAsync().ConfigureAwait(false);

                    saved = true;
                }
                catch (DbUpdateException ex) when(ex?.InnerException is MySqlException mex && mex.Number == 1062)
                {
                }
            } while (!saved);
            return(channel);
        }

        #endregion
    }
Exemplo n.º 3
0
        public async Task <Session> AddSession(Session session)
        {
            bool saved = false;

            do
            {
                try
                {
                    long id = SkynetRandom.Id();
                    session.SessionId = id;
                    Sessions.Add(session);
                    await SaveChangesAsync().ConfigureAwait(false);

                    saved = true;
                }
                catch (DbUpdateException ex) when(ex?.InnerException is MySqlException mex && mex.Number == 1062)
                {
                }
            } while (!saved);
            return(session);
        }
Exemplo n.º 4
0
        public async Task <(Account, MailConfirmation, bool)> AddAccount(string mailAddress, byte[] passwordHash)
        {
            Account account = new Account {
                PasswordHash = passwordHash
            };
            MailConfirmation confirmation = new MailConfirmation {
                Account = account, MailAddress = mailAddress
            };

            bool saved = false;

            do
            {
                try
                {
                    long   id    = SkynetRandom.Id();
                    string token = SkynetRandom.String(10);
                    account.AccountId  = id;
                    confirmation.Token = token;
                    Accounts.Add(account);
                    MailConfirmations.Add(confirmation);
                    await SaveChangesAsync().ConfigureAwait(false);

                    saved = true;
                }
                catch (DbUpdateException ex) when(ex?.InnerException is MySqlException mex && mex.Number == 1062)
                {
                    // Return false if unique constraint violation is caused by the mail address
                    // An example for mex.Message is "Duplicate entry '*****@*****.**' for key 'PRIMARY'"

                    if (mex.Message.Contains('@', StringComparison.Ordinal))
                    {
                        return(null, null, false);
                    }
                }
            } while (!saved);
            return(account, confirmation, true);
        }