public async Task TestMethod_SignUpWithTelegram_SuccessRegistration()
        {
            //Arrange
            AccountRegistrationService registrationService = new AccountRegistrationService(dbContext);
            TelegramLoginInfo          telegramLoginInfo   = new TelegramLoginInfo
            {
                TelegramId = Int32.MaxValue / 2
            };
            string name = "Muhammad";

            //Act
            await registrationService.RegisterAccountAsync(name, telegramLoginInfo);

            //Assert
            var telegramLoginInfoDb = await dbContext.TelegramLoginInfo
                                      .Where(info => info.TelegramId == telegramLoginInfo.TelegramId)
                                      .Include(info => info.Account)
                                      .SingleAsync();

            var account = telegramLoginInfo.Account;

            Assert.AreEqual(name, account.Name);
            Assert.AreEqual(telegramLoginInfo.TelegramId, account.TelegramLoginInfo.TelegramId);
            Assert.IsTrue(account.OrderStatusGroups.Count > 0);
        }
Ejemplo n.º 2
0
        public void TestMethod_SignUpWithNullName_RegistrationFailure()
        {
            //Arrange
            var dbContext = InMemoryDatabaseFactory.Create();
            AccountRegistrationService registrationService = new AccountRegistrationService(dbContext);
            TelegramLoginInfo          telegramLoginInfo   = new TelegramLoginInfo
            {
                TelegramId = Int32.MaxValue / 2
            };

            //Act
            registrationService.RegisterAccountAsync(null, telegramLoginInfo).Wait();
        }
        public static bool HasOneLoginInfo([CanBeNull] EmailLoginInfo emailLoginInfo,
                                           [CanBeNull] TelegramLoginInfo telegramLoginInfo)
        {
            if (telegramLoginInfo != null)
            {
                return(true);
            }

            if (emailLoginInfo != null)
            {
                CheckEmailLoginInfo(emailLoginInfo);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        public void TestMethod_SignUpWithTelegram_SuccessRegistration()
        {
            //Arrange
            var dbContext = InMemoryDatabaseFactory.Create();
            AccountRegistrationService registrationService = new AccountRegistrationService(dbContext);
            TelegramLoginInfo          telegramLoginInfo   = new TelegramLoginInfo
            {
                TelegramId = Int32.MaxValue / 2
            };
            string name = "Muhammad";

            //Act
            registrationService.RegisterAccountAsync(name, telegramLoginInfo).Wait();

            //Assert
            var account = dbContext.Accounts
                          .Include(account1 => account1.TelegramLoginInfo)
                          .Include(account1 => account1.OrderStatusGroups)
                          .First();

            Assert.AreEqual(name, account.Name);
            Assert.AreEqual(telegramLoginInfo.TelegramId, account.TelegramLoginInfo.TelegramId);
            Assert.IsTrue(account.OrderStatusGroups.Count > 0);
        }
        private async Task <Account> RegisterAccountAsync(string name, EmailLoginInfo emailLoginInfo, TelegramLoginInfo telegramLoginInfo)
        {
            //TODO добавить сервисы для проверки входных данных
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(nameof(name));
            }

            if (!LoginInfoCheckService.HasOneLoginInfo(emailLoginInfo, telegramLoginInfo))
            {
                throw new Exception("empty login info");
            }

            var account = new Account
            {
                Name              = name,
                RegistrationDate  = DateTime.UtcNow,
                Money             = 49.99M,
                EmailLoginInfo    = emailLoginInfo,
                TelegramLoginInfo = telegramLoginInfo,
                OrderStatusGroups =
                {
                    new OrderStatusGroup
                    {
                        Name          = "Стандартный набор статусов",
                        OrderStatuses = new[]
                        {
                            new OrderStatus {
                                Name = "Просмотрено", Message = ""
                            },
                            new OrderStatus {
                                Name = "⏳В обработке", Message = "⏳Ваш заказ находится в обработке."
                            },
                            new OrderStatus {
                                Name = "🚚В пути", Message = "🚚Ваш заказ в пути."
                            },
                            new OrderStatus {
                                Name = "✅Принят", Message = "✅Ваш заказ был принят."
                            },
                            new OrderStatus {
                                Name = "❌Отменён", Message = "❌Ваш заказ был отменён."
                            }
                        }
                    }
                }
            };

            await dbContext.Accounts.AddAsync(account);

            await dbContext.SaveChangesAsync();

            return(account);
        }
 public async Task <Account> RegisterAccountAsync(string name, TelegramLoginInfo telegramLoginInfo)
 {
     return(await RegisterAccountAsync(name, null, telegramLoginInfo));
 }
        public async Task <IActionResult> LoginWithTelegram(string id,
                                                            [FromQuery(Name = "first_name")] string firstName,
                                                            [FromQuery(Name = "last_name")]  string lastName,
                                                            [FromQuery(Name = "username")]   string username,
                                                            [FromQuery(Name = "photo_url")]  string photoUrl,
                                                            [FromQuery(Name = "auth_date")]  string authDate,
                                                            [FromQuery(Name = "hash")]       string hash)
        {
            //@bots_constructor_bot
            string botToken = "913688656:AAH5l-ZAOmpI5MDqcq3ye1M1CF8ESF-Bms8";

            List <string> myList = new List <string>
            {
                $"id={id}",
                $"auth_date={authDate}"
            };

            //Эти поля могут быть пустыми
            if (firstName != null)
            {
                myList.Add($"first_name={firstName}");
            }
            if (lastName != null)
            {
                myList.Add($"last_name={lastName}");
            }
            if (username != null)
            {
                myList.Add($"username={username}");
            }
            if (photoUrl != null)
            {
                myList.Add($"photo_url={photoUrl}");
            }

            string[] myArr = myList.ToArray();
            Array.Sort(myArr);
            string dataCheckString = string.Join("\n", myArr);

            var authorizationIsValid = CheckTelegramAuthorization(hash, botToken, dataCheckString);

            if (authorizationIsValid)
            {
                int.TryParse(id, out int telegramId);

                var account = context.TelegramLoginInfo
                              .Include(info => info.Account)
                              .SingleOrDefault(_acc => _acc.TelegramId == telegramId)
                              ?.Account;


                if (account == null)
                {
                    string            name = firstName + " " + lastName;
                    TelegramLoginInfo telegramLoginInfo = new TelegramLoginInfo
                    {
                        TelegramId = telegramId
                    };

                    account = await registrationService.RegisterAccountAsync(name, telegramLoginInfo);
                }


                if (account != null)
                {
                    Authenticate(account);
                    return(RedirectToAction("MyBots", "MyBots"));
                }
                else
                {
                    return(RedirectToAction("Failure", "StaticMessage", new { message = "Что-то пошло не так((" }));
                }

                //TODO Отправить сообщение о авторизации пользователю (приветствие)
            }

            ModelState.AddModelError("", "Ошибка авторизации");
            return(RedirectToAction("Login", "SignIn"));
        }