Example #1
0
        public async Task <IActionResult> Register([FromBody] AccountViewModel creds)
        {
            if (await _userStore.FindByLogin(creds.Login, _cts.Token) != null)
            {
                return(BadRequest(new { errorText = "This account already exist" }));
            }

            var newUser = new Models.User()
            {
                Login = creds.Login
            };
            var hashedUserPassword = _passwordHasher.HashPassword(newUser, creds.Password);

            newUser.PasswordHash = hashedUserPassword;

            var createResult = await _userStore.CreateAsync(newUser, _cts.Token);

            if (await _userStore.CreateAsync(newUser, _cts.Token) == IdentityResult.Success)
            {
                var identity = GetIdentity(newUser);

                return(new JsonResult(
                           new
                {
                    responseType = HttpStatusCode.OK,
                    token = GetJwt(identity),
                    accountCreds = new { Login = creds.Login },
                    Message = "You're logged in"
                }));
            }

            return(BadRequest(new { errorText = $"Something goes wrong. \n{createResult.Errors.ToList()}" }));
        }
Example #2
0
        public async Task CreateInvalidUser()
        {
            StubData();

            // user to create
            user = new ApplicationUser
            {
                Email    = "*****@*****.**",
                UserName = "******"
            };

            using (var factory = new ApplicationDbContextFactory())
            {
                using (var dbContext = factory.CreateContext(false))
                {
                    using (var userStore = new ApplicationUserStore(dbContext))
                    {
                        await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() =>
                        {
                            await userStore.CreateAsync(user, driverLicense, billingAddress, creditCard);
                        });
                    }
                }
            }
        }
Example #3
0
        public async Task UpdateDriverLicense()
        {
            StubData();

            using (var factory = new ApplicationDbContextFactory())
            {
                using (var dbContext = factory.CreateContext(false))
                {
                    using (var userStore = new ApplicationUserStore(dbContext))
                    {
                        var result = await userStore.CreateAsync(user, this.driverLicense, billingAddress, creditCard);

                        Assert.IsTrue(result.Succeeded);
                    }
                }

                DriverLicense driverLicense;
                using (var dbContext = factory.CreateContext(true))
                {
                    using (var userStore = new ApplicationUserStore(dbContext))
                    {
                        var applicationUser = await userStore.FindUserWithDriverLicenseAsync(user.Id);

                        Assert.IsNotNull(applicationUser);

                        driverLicense = new DriverLicense
                        {
                            Id = applicationUser.DriverLicenseId,
                            DriverLicenseNumber = "70001",
                            CountryOfIssue      = this.driverLicense.CountryOfIssue
                        };

                        var result = await userStore.UpdateDriverLicenseAsync(applicationUser, driverLicense);

                        Assert.IsTrue(result.Succeeded);
                    }
                }

                using (var dbContext = factory.CreateContext(false))
                {
                    var applicationUser = await dbContext.Users
                                          .Include(u => u.DriverLicense)
                                          .SingleOrDefaultAsync(u => u.UserName == user.UserName);

                    Assert.IsNotNull(applicationUser.DriverLicense);
                    Assert.IsTrue(applicationUser.DriverLicenseId == applicationUser.DriverLicense.Id);
                    Assert.IsTrue(applicationUser.DriverLicense.DriverLicenseNumber == driverLicense.DriverLicenseNumber);
                    Assert.IsTrue(applicationUser.DriverLicense.CountryOfIssue == driverLicense.CountryOfIssue);
                }
            }
        }
Example #4
0
        public async Task CreateNewUser()
        {
            StubData();

            using (var factory = new ApplicationDbContextFactory())
            {
                using (var dbContext = factory.CreateContext(true))
                {
                    using (var userStore = new ApplicationUserStore(dbContext))
                    {
                        var result = await userStore.CreateAsync(user, driverLicense, billingAddress, creditCard);

                        Assert.IsTrue(result.Succeeded);
                    }
                }

                using (var dbContext = factory.CreateContext(false))
                {
                    var applicationUser = await dbContext.Users
                                          .Include(u => u.DriverLicense)
                                          .Include(u => u.CreditCard).ThenInclude(cc => cc.BillingAddress)
                                          .SingleOrDefaultAsync(u => u.UserName == user.UserName);

                    Assert.IsNotNull(applicationUser);
                    Assert.IsTrue(applicationUser.Email == user.Email);
                    Assert.IsTrue(applicationUser.Name.Equals(user.Name));

                    Assert.IsNotNull(applicationUser.DriverLicense);
                    Assert.IsTrue(applicationUser.DriverLicenseId == applicationUser.DriverLicense.Id);
                    Assert.IsTrue(applicationUser.DriverLicense.DriverLicenseNumber == driverLicense.DriverLicenseNumber);
                    Assert.IsTrue(applicationUser.DriverLicense.CountryOfIssue == driverLicense.CountryOfIssue);

                    Assert.IsNotNull(applicationUser.CreditCard);
                    Assert.IsTrue(applicationUser.CreditCardId == applicationUser.CreditCard.Id);
                    Assert.IsTrue(applicationUser.CreditCard.Type == creditCard.Type);
                    Assert.IsTrue(applicationUser.CreditCard.CreditCardNumber == creditCard.CreditCardNumber);
                    Assert.IsTrue(applicationUser.CreditCard.NameOnCard == creditCard.NameOnCard);

                    Assert.IsNotNull(applicationUser.CreditCard.BillingAddress);
                    Assert.IsTrue(applicationUser.CreditCard.BillingAddressId == applicationUser.CreditCard.BillingAddress.Id);
                    Assert.IsTrue(applicationUser.CreditCard.BillingAddress.StreetAddress == billingAddress.StreetAddress);
                    Assert.IsTrue(applicationUser.CreditCard.BillingAddress.SuiteNumber == billingAddress.SuiteNumber);
                    Assert.IsTrue(applicationUser.CreditCard.BillingAddress.City == billingAddress.City);
                }
            }
        }
        public async Task Given_an_ApplicationUserStore_when_a_user_is_created_then_it_can_be_retrieved_by_username()
        {
            // GIVEN a user store and user
            var userStore = new ApplicationUserStore(_authContext.Object);

            var user = new ApplicationUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };

            // WHEN the user is created
            await userStore.CreateAsync(user, false);

            // THEN the user should retrieved from the user store and match the original user
            var userFromStore = await userStore.FindByNameAsync(user.UserName, false);

            userFromStore.UserName.Should().Be(user.UserName, "because the user has been created in and retrieved from the ApplicationUserStore.");
        }
        public void GetAll_WithUsers()
        {
            ApplicationUser[] users = new ApplicationUser[]
            {
                new ApplicationUser
                {
                    UserName = "******",
                    Email    = "user1email"
                },
                new ApplicationUser
                {
                    UserName = "******",
                    Email    = "user2email"
                },
                new ApplicationUser
                {
                    UserName = "******",
                    Email    = "user3email"
                },
            };

            Dictionary <string, ApplicationUser> dictUsers = new Dictionary <string, ApplicationUser>();

            foreach (var user in users)
            {
                dictUsers[user.UserName] = user;
                _applicationUserStore.CreateAsync(user).Wait();
            }

            var usersFromStore = new List <ApplicationUser>(_applicationUserStore.GetAll().GetAwaiter().GetResult());

            Assert.AreEqual(dictUsers.Count, usersFromStore.Count);
            foreach (var userFromStore in usersFromStore)
            {
                Assert.IsTrue(dictUsers.ContainsKey(userFromStore.UserName));
                Assert.AreEqual(dictUsers[userFromStore.UserName].Email, userFromStore.Email);
            }
        }
        public async Task Given_an_ApplicationUserStore_when_a_user_is_updated_then_it_can_be_retrieved()
        {
            // Given an ApplicationUserStore and ApplicationUser
            var userStore = new ApplicationUserStore(_authContext.Object);
            var user      = new ApplicationUser
            {
                UserName = "******",
                Email    = "*****@*****.**"
            };

            // WHEN creating and upating a user.
            await userStore.CreateAsync(user, false);

            var userFromStore = await userStore.FindByNameAsync(user.UserName, false);

            userFromStore.UserName.Should().Be(user.UserName, "because the user has been created in and retrieved from the userstore.");

            // THEN the user should be the same in the store after being updated.
            await userStore.UpdateAsync(user, false);

            userFromStore.UserName.Should().Be(user.UserName, "because the user has been updated in the userstore.");
            await userStore.DeleteAsync(user, false);
        }
        public async Task<ApplicationUser> Authorize(string token)
        {
            var fbClient = new FacebookClient(token);
            dynamic me = await fbClient.GetTaskAsync("me");

            var adminEmail = CloudConfigurationManager.GetSetting("email");

            try
            {
                var userStore = new ApplicationUserStore();

                var user = await userStore.FindByIdAsync(me.id);
                if (user == null)
                {
                    var newUser = new ApplicationUser()
                                      {
                                          Email = me.email,
                                          Id = me.id,
                                          UserName = me.email,
                                          Role = adminEmail == me.email ? "Admin" : "User",
                                          FirstName = me.first_name,
                                          LastName = me.last_name
                                      };

                    await userStore.CreateAsync(newUser);

                    return newUser;
                }

                return user;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return null;
            }
        }