Example #1
0
        public async Task Can_Create_User_With_Password()
        {
            // Arrange
            var user = _userFaker.Generate();

            var serializer = _fixture.GetSerializer <User>(p => new { p.Password, p.Username });


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

            var request = new HttpRequestMessage(httpMethod, route)
            {
                Content = new StringContent(serializer.Serialize(user))
            };

            request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.MediaType);

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

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

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

            var returnedUser = _fixture.GetDeserializer().DeserializeSingle <User>(body).Data;
            var document     = JsonConvert.DeserializeObject <Document>(body);

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

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

            Assert.Equal(user.Username, dbUser.Username);
            Assert.Equal(user.Password, dbUser.Password);
        }
Example #2
0
        public async Task Can_Post_TodoItem()
        {
            // Arrange
            var person = new Person();

            _context.People.Add(person);
            await _context.SaveChangesAsync();

            var serializer = _fixture.GetSerializer <TodoItem>(e => new { e.Description, e.OffsetDate, e.Ordinal, e.CreatedDate }, e => new { e.Owner });

            var todoItem  = _todoItemFaker.Generate();
            var nowOffset = new DateTimeOffset();

            todoItem.OffsetDate = nowOffset;

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

            var request = new HttpRequestMessage(httpMethod, route)
            {
                Content = new StringContent(serializer.Serialize(todoItem))
            };

            request.Content.Headers.ContentType = new MediaTypeHeaderValue(HeaderConstants.MediaType);

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

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            var body = await response.Content.ReadAsStringAsync();

            var deserializedBody = _fixture.GetDeserializer().DeserializeSingle <TodoItemClient>(body).Data;

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.Equal(todoItem.Description, deserializedBody.Description);
            Assert.Equal(todoItem.CreatedDate.ToString("G"), deserializedBody.CreatedDate.ToString("G"));
            Assert.Equal(nowOffset, deserializedBody.OffsetDate);
            Assert.Null(deserializedBody.AchievedDate);
        }