コード例 #1
0
        public async Task CreateAsync_ReturnsCreatedUser()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = new User
            {
                FirstName = "John",
                LastName  = "Doe",
                Email     = "*****@*****.**",
                UserName  = "******"
            };

            // Act
            User result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.CreateAsync(user);
            }

            // Assert
            Assert.True(result.CreatedOn > DateTime.Now.AddMinutes(-1));
            Assert.Equal(user.Email, result.Email);
            Assert.Equal(user.FirstName, result.FirstName);
            Assert.Equal(user.LastName, result.LastName);
            Assert.Equal(user.UserName, result.UserName);
        }
コード例 #2
0
        public async Task DeleteAsync_WhenUserExists_ReturnsTrue()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = GetSampleUser();

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddAsync(user);

                await dbContext.SaveChangesAsync();
            }

            // Act
            bool result;
            User expectedUser;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.DeleteAsync(user.Id);

                expectedUser = dbContext.Users.SingleOrDefault(u => u.Id == user.Id);
            }

            // Assert
            Assert.True(result);
            Assert.Null(expectedUser);
        }
コード例 #3
0
        public async Task GetAsync_ReturnsUsers()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var users            = new List <User>
            {
                GetSampleUser(),
                GetSampleUser(),
                GetSampleUser()
            };

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddRangeAsync(users);

                await dbContext.SaveChangesAsync();
            }

            // Act
            List <User> result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.GetAsync();
            }

            // Assert
            Assert.Equal(users.Count, result.Count);
            foreach (var user in result)
            {
                var expected = users.SingleOrDefault(u => u.Id == user.Id);
                AssertUser(expected, user);
            }
        }
コード例 #4
0
        public async Task GetByIdAsync_ReturnsUser()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = GetSampleUser();

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddAsync(user);

                await dbContext.SaveChangesAsync();
            }

            // Act
            User result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.GetByIdAsync(user.Id);
            }

            // Assert
            AssertUser(user, result);
        }
コード例 #5
0
        public async Task Initialize_ShouldCreateAUser()
        {
            var optionsBuilder = new DbContextOptionsBuilder <WebApiCoreSeedContext>();

            optionsBuilder.UseInMemoryDatabase("GetByIdAsync_ShouldReturnUser");
            using (var dbContext = new WebApiCoreSeedContext(optionsBuilder.Options))
            {
                DatabaseSeed.Initialize(dbContext);

                Assert.Equal(1, await dbContext.Users.CountAsync());
            }
        }
コード例 #6
0
        public async Task GetByIdAsync_ReturnsNull_WhenNotExists()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();

            // Act
            User result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.GetByIdAsync(Guid.NewGuid());
            }

            // Assert
            Assert.Null(result);
        }
コード例 #7
0
        public async Task DeleteAsync_WhenUserNotExists_ReturnsFalse()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();

            // Act
            bool result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.DeleteAsync(Guid.NewGuid());
            }

            // Assert
            Assert.False(result);
        }
コード例 #8
0
        public async Task GetByIdAsync_ShouldReturnNull()
        {
            // Arrange
            var optionsBuilder = new DbContextOptionsBuilder <WebApiCoreSeedContext>();

            optionsBuilder.UseInMemoryDatabase("GetByIdAsync_ShouldReturnNull");
            User user;

            using (var context = new WebApiCoreSeedContext(optionsBuilder.Options))
            {
                var userService = new UserService(context);

                // Act
                user = await userService.GetByIdAsync(Guid.Parse("00000000-0000-0000-0000-000000038479"));
            }

            // Assert
            Assert.Null(user);
        }
コード例 #9
0
        public async Task GetByIdAsync_ShouldReturnUser()
        {
            // Arrange
            var optionsBuilder = new DbContextOptionsBuilder <WebApiCoreSeedContext>();

            optionsBuilder.UseInMemoryDatabase("GetByIdAsync_ShouldReturnUser");
            var createdUser =
                new User
            {
                Id        = new Guid("d58893fd-3836-4308-b840-85f4fe548264"),
                FirstName = "John",
                LastName  = "Doe",
                Email     = "*****@*****.**",
                UserName  = "******"
            };

            User retrievedUser = null;

            using (var context = new WebApiCoreSeedContext(optionsBuilder.Options))
            {
                context.Add(createdUser);
                context.SaveChanges();
            }

            using (var context = new WebApiCoreSeedContext(optionsBuilder.Options))
            {
                var userService = new UserService(context);

                // Act
                retrievedUser = await userService.GetByIdAsync(createdUser.Id);
            }

            // Assert
            Assert.NotNull(retrievedUser);
            Assert.Equal(createdUser.UserName, retrievedUser.UserName);
            Assert.Equal(createdUser.Email, retrievedUser.Email);
            Assert.Equal(createdUser.FirstName, retrievedUser.FirstName);
            Assert.Equal(createdUser.LastName, retrievedUser.LastName);
        }
コード例 #10
0
        public async Task UpdateAsync_ReturnsUpdatedUser()
        {
            // Arrange
            var dbContextOptions = DbContextHelper.GetOptions <WebApiCoreSeedContext>();
            var user             = GetSampleUser();

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                await dbContext.Users.AddAsync(user);

                await dbContext.SaveChangesAsync();
            }

            var toUpdate = new User
            {
                Id        = user.Id,
                FirstName = $"Updated {user.FirstName}",
                LastName  = $"Updated {user.LastName}",
                Email     = "*****@*****.**",
                UserName  = $"updated-{user.UserName}"
            };

            // Act
            User result;

            using (var dbContext = new WebApiCoreSeedContext(dbContextOptions))
            {
                var userService = new UserService(dbContext);
                result = await userService.UpdateAsync(toUpdate);
            }

            // Assert
            Assert.True(result.UpdatedOn > DateTime.Now.AddMinutes(-1));
            Assert.Equal(toUpdate.Email, result.Email);
            Assert.Equal(toUpdate.FirstName, result.FirstName);
            Assert.Equal(toUpdate.LastName, result.LastName);
            Assert.Equal(toUpdate.UserName, result.UserName);
        }
コード例 #11
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, WebApiCoreSeedContext dbContext)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMiddleware(typeof(ErrorHandlingMiddleware));
            app.UseMiddleware(typeof(AuthorizationMiddleware));

            //Enable swagger midleware
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                //Sets swagger UI route on root, "GET {baseUrl}/ "
                c.RoutePrefix = string.Empty;
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiCoreSeed V1");
            });

            app.UseMvc();
            DatabaseSeed.Initialize(dbContext);
        }
コード例 #12
0
 public UserService(WebApiCoreSeedContext dbContext)
 {
     _dbContext = dbContext;
 }
コード例 #13
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, WebApiCoreSeedContext dbContext)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            var jwtOptions = new JwtBearerOptions
            {
                Audience  = Configuration["auth0:clientId"],
                Authority = $"https://{Configuration["auth0:domain"]}/",
            };

            app.UseMiddleware(typeof(ErrorHandlingMiddleware));
            app.UseMiddleware(typeof(AuthorizationMiddleware));
            app.UseJwtBearerAuthentication(jwtOptions);

            app.UseMvc();
            DatabaseSeed.Initialize(dbContext);
        }