示例#1
0
        public async Task Can_Fetch_Many_To_Many_Through_Id()
        {
            // Arrange
            var context    = _fixture.GetService <AppDbContext>();
            var article    = _articleFaker.Generate();
            var tag        = _tagFaker.Generate();
            var articleTag = new ArticleTag
            {
                Article = article,
                Tag     = tag
            };

            context.ArticleTags.Add(articleTag);
            await context.SaveChangesAsync();

            var route = $"/api/v1/articles/{article.Id}/tags";

            // @TODO - Use fixture
            var builder = WebHost.CreateDefaultBuilder()
                          .UseStartup <TestStartup>();
            var server = new TestServer(builder);
            var client = server.CreateClient();

            // Act
            var response = await client.GetAsync(route);

            // Assert
            var body = await response.Content.ReadAsStringAsync();

            Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");

            var document = JsonConvert.DeserializeObject <Document>(body);

            Assert.Single(document.ManyData);

            var tagResponse = _fixture.GetDeserializer().DeserializeMany <Tag>(body).Data.First();

            Assert.NotNull(tagResponse);
            Assert.Equal(tag.Id, tagResponse.Id);
            Assert.Equal(tag.Name, tagResponse.Name);
        }
示例#2
0
 public ResourceDefinitionTests(TestFixture <Startup> fixture)
 {
     _fixture   = fixture;
     _context   = fixture.GetService <AppDbContext>();
     _userFaker = new Faker <User>()
                  .RuleFor(u => u.Username, f => f.Internet.UserName())
                  .RuleFor(u => u.Password, f => f.Internet.Password());
     _todoItemFaker = new Faker <TodoItem>()
                      .RuleFor(t => t.Description, f => f.Lorem.Sentence())
                      .RuleFor(t => t.Ordinal, f => f.Random.Number())
                      .RuleFor(t => t.CreatedDate, f => f.Date.Past());
     _personFaker = new Faker <Person>()
                    .RuleFor(p => p.FirstName, f => f.Name.FirstName())
                    .RuleFor(p => p.LastName, f => f.Name.LastName());
 }
        public ManyToManyTests(TestFixture <TestStartup> fixture)
        {
            _fixture = fixture;
            var context = _fixture.GetService <AppDbContext>();

            _authorFaker = new Faker <Author>()
                           .RuleFor(a => a.LastName, f => f.Random.Words(2));

            _articleFaker = new Faker <Article>()
                            .RuleFor(a => a.Caption, f => f.Random.AlphaNumeric(10))
                            .RuleFor(a => a.Author, f => _authorFaker.Generate());

            _tagFaker = new Faker <Tag>()
                        .CustomInstantiator(f => new Tag())
                        .RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
        }
        public InjectableResourceTests(TestFixture <TestStartup> fixture)
        {
            _fixture = fixture;
            _context = fixture.GetService <AppDbContext>();

            _todoItemFaker = new Faker <TodoItem>()
                             .RuleFor(t => t.Description, f => f.Lorem.Sentence())
                             .RuleFor(t => t.Ordinal, f => f.Random.Number())
                             .RuleFor(t => t.CreatedDate, f => f.Date.Past());
            _personFaker = new Faker <Person>()
                           .RuleFor(t => t.FirstName, f => f.Name.FirstName())
                           .RuleFor(t => t.LastName, f => f.Name.LastName());
            _passportFaker = new Faker <Passport>()
                             .CustomInstantiator(f => new Passport(_context))
                             .RuleFor(t => t.SocialSecurityNumber, f => f.Random.Number(100, 10_000));
            _countryFaker = new Faker <Country>()
                            .RuleFor(c => c.Name, f => f.Address.Country());
            _visaFaker = new Faker <Visa>()
                         .RuleFor(v => v.ExpiresAt, f => f.Date.Future());
        }
 public ResourceDefinitionTests(TestFixture <TestStartup> fixture)
 {
     _fixture     = fixture;
     _context     = fixture.GetService <AppDbContext>();
     _authorFaker = new Faker <Author>()
                    .RuleFor(a => a.Name, f => f.Random.Words(2));
     _articleFaker = new Faker <Article>()
                     .RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10))
                     .RuleFor(a => a.Author, f => _authorFaker.Generate());
     _userFaker = new Faker <User>()
                  .CustomInstantiator(f => new User(_context))
                  .RuleFor(u => u.Username, f => f.Internet.UserName())
                  .RuleFor(u => u.Password, f => f.Internet.Password());
     _todoItemFaker = new Faker <TodoItem>()
                      .RuleFor(t => t.Description, f => f.Lorem.Sentence())
                      .RuleFor(t => t.Ordinal, f => f.Random.Number())
                      .RuleFor(t => t.CreatedDate, f => f.Date.Past());
     _personFaker = new Faker <Person>()
                    .RuleFor(p => p.FirstName, f => f.Name.FirstName())
                    .RuleFor(p => p.LastName, f => f.Name.LastName());
     _tagFaker = new Faker <Tag>()
                 .CustomInstantiator(f => new Tag(_context))
                 .RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
 }
        public async Task Can_Get_TodoItems()
        {
            // Arrange
            const int expectedEntitiesPerPage = 5;
            var       person   = new Person();
            var       todoItem = _todoItemFaker.Generate();

            todoItem.Owner = person;
            _context.TodoItems.Add(todoItem);
            _context.SaveChanges();

            var httpMethod = new HttpMethod("GET");
            var route      = "/api/v1/todo-items";
            var request    = new HttpRequestMessage(httpMethod, route);

            // Act
            var response = await _fixture.Client.SendAsync(request);

            var body = await response.Content.ReadAsStringAsync();

            var deserializedBody = _fixture.GetService <IJsonApiDeSerializer>().DeserializeList <TodoItem>(body);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.NotEmpty(deserializedBody);
            Assert.True(deserializedBody.Count <= expectedEntitiesPerPage);
        }
        public async Task Can_Fetch_Many_To_Many_Through_All()
        {
            // arrange
            var context = _fixture.GetService <AppDbContext>();
            var article = _articleFaker.Generate();
            var tag     = _tagFaker.Generate();

            context.Articles.RemoveRange(context.Articles);
            await context.SaveChangesAsync();

            var articleTag = new ArticleTag
            {
                Article = article,
                Tag     = tag
            };

            context.ArticleTags.Add(articleTag);
            await context.SaveChangesAsync();

            var route = $"/api/v1/articles?include=tags";

            // act
            var response = await _fixture.Client.GetAsync(route);

            // assert
            var body = await response.Content.ReadAsStringAsync();

            Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with payload: {body}");

            var document = JsonConvert.DeserializeObject <Documents>(body);

            Assert.NotEmpty(document.Included);

            var articleResponseList = _fixture.GetService <IJsonApiDeSerializer>().DeserializeList <Article>(body);

            Assert.NotNull(articleResponseList);

            var articleResponse = articleResponseList.FirstOrDefault(a => a.Id == article.Id);

            Assert.NotNull(articleResponse);
            Assert.Equal(article.Name, articleResponse.Name);

            var tagResponse = Assert.Single(articleResponse.Tags);

            Assert.Equal(tag.Id, tagResponse.Id);
            Assert.Equal(tag.Name, tagResponse.Name);
        }
        public async Task Can_Create_User_With_Password()
        {
            // Arrange
            var user    = _userFaker.Generate();
            var content = new
            {
                data = new
                {
                    type       = "users",
                    attributes = new Dictionary <string, object>()
                    {
                        { "username", user.Username },
                        { "password", user.Password },
                    }
                }
            };

            var httpMethod = new HttpMethod("POST");
            var route      = $"/api/v1/users";

            var request = new HttpRequestMessage(httpMethod, route);

            request.Content = new StringContent(JsonConvert.SerializeObject(content));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

            // Act
            var response = await _fixture.Client.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);

            // response assertions
            var body = await response.Content.ReadAsStringAsync();

            var deserializedBody = (User)_fixture.GetService <IJsonApiDeSerializer>().Deserialize(body);
            var document         = JsonConvert.DeserializeObject <Document>(body);

            Assert.False(document.Data.Attributes.ContainsKey("password"));
            Assert.Equal(user.Username, document.Data.Attributes["username"]);

            // db assertions
            var dbUser = await _context.Users.FindAsync(deserializedBody.Id);

            Assert.Equal(user.Username, dbUser.Username);
            Assert.Equal(user.Password, dbUser.Password);
        }