Example #1
0
        public async Task Test03()
        {
            int chiefOfStaffId = Shared.Get <Role>("Role_ChiefOfStaff").Id;

            // Prepare a well formed entity
            var dtoForSave = new UserForSave
            {
                Name  = "Ahmad Akra",
                Name2 = "أحمد عكره",
                Email = "*****@*****.**",
                Roles = new List <RoleMembershipForSave>
                {
                    new RoleMembershipForSave
                    {
                        RoleId = chiefOfStaffId,
                        Memo   = "Nice"
                    }
                }
            };

            // Save it
            var dtosForSave = new List <UserForSave> {
                dtoForSave
            };
            var response = await Client.PostAsJsonAsync($"{Url}?expand=Roles/Role", dtosForSave);

            // Assert that the response status code is a happy 200 OK
            Output.WriteLine(await response.Content.ReadAsStringAsync());
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            // Assert that the response is well-formed singleton
            var responseData = await response.Content.ReadAsAsync <EntitiesResponse <User> >();

            Assert.Single(responseData.Result);

            // Assert that the result matches the saved entity
            Assert.Equal("User", responseData.CollectionName);

            var responseDto = responseData.Result.FirstOrDefault();

            Assert.NotNull(responseDto?.Id);
            Assert.Equal(dtoForSave.Name, responseDto.Name);
            Assert.Equal(dtoForSave.Name2, responseDto.Name2);
            Assert.Equal(dtoForSave.Email, responseDto.Email);
            Assert.Collection(responseDto.Roles,
                              p =>
            {
                Assert.Equal(dtoForSave.Roles[0].RoleId, p.RoleId);
                Assert.Equal(dtoForSave.Roles[0].Memo, p.Memo);
                Assert.NotEqual(0, p.Id);
            }
                              );

            Shared.Set("Users_AhmadAkra", responseDto);
        }
Example #2
0
        public async Task Test05()
        {
            // Prepare a record with the same code as one that has been saved already
            var dtoForSave = new UserForSave
            {
                Name  = "Abdullah Ulber",
                Email = "abdullah-ulber", // Wrong email
                Roles = new List <RoleMembershipForSave>
                {
                    new RoleMembershipForSave
                    {
                        RoleId = 9999, // non existent Id
                        Memo   = "Nice"
                    }
                }
            };

            // Call the API
            var response = await Client.PostAsJsonAsync(Url, new List <UserForSave> {
                dtoForSave
            });

            // Assert that the response status code is 422 unprocessable entity (validation errors)
            Output.WriteLine(await response.Content.ReadAsStringAsync());
            Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);

            // Confirm that the result is a well-formed validation errors structure
            var errors = await response.Content.ReadAsAsync <ValidationErrors>();

            // Assert that it contains a validation key pointing to the Email property
            {
                string expectedKey = "[0].Email";
                Assert.True(errors.ContainsKey(expectedKey), $"Expected error key '{expectedKey}' was not found");

                // Assert that it contains a useful error message in English
                var message = errors[expectedKey].Single();
                Assert.Contains("not a valid e-mail address", message.ToLower());
            }

            // Fix the email
            dtoForSave.Email = "*****@*****.**";
            response         = await Client.PostAsJsonAsync(Url, new List <UserForSave> {
                dtoForSave
            });

            // Assert that the response status code is 422 unprocessable entity (validation errors)
            Output.WriteLine(await response.Content.ReadAsStringAsync());
            Assert.Equal(HttpStatusCode.UnprocessableEntity, response.StatusCode);

            // Confirm that the result is a well-formed validation errors structure
            errors = await response.Content.ReadAsAsync <ValidationErrors>();

            // Assert that it contains a validation key pointing to the RoleId property of the Role line
            {
                string expectedKey = "[0].Roles[0].RoleId";
                Assert.True(errors.ContainsKey(expectedKey), $"Expected error key '{expectedKey}' was not found");

                // Assert that it contains a useful error message in English
                var message = errors[expectedKey].Single();
                Assert.Contains("no longer exists", message.ToLower());
            }
        }