Exemple #1
0
        public async Task Test3102()
        {
            await CreateNewRole(); // To create a new role

            int salesManagerId = _shared.GetItem <Role>("Role_SalesManager").Id.Value;

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

            // Save it
            var dtosForSave = new List <LocalUserForSave> {
                dtoForSave
            };
            var response = await _client.PostAsJsonAsync($"{localUsersURL}?expand=Roles", 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 <LocalUser> >();

            Assert.Single(responseData.Data);

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

            var responseDto = responseData.Data.FirstOrDefault();

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

            _shared.SetItem("LocalUsers_AhmadAkra", responseDto);
        }
Exemple #2
0
        public async Task Test3104()
        {
            int salesManagerId = _shared.GetItem <Role>("Role_SalesManager").Id.Value;

            // Prepare a unit with the same code as one that has been saved already
            var dtoForSave = new LocalUserForSave
            {
                EntityState = "Inserted",
                Name        = "Abdullah Ulber",
                Name2       = "عبد الله ألبر",
                Email       = "abdullah-ulber", // Wrong email
                AgentId     = null,
                Roles       = new List <RoleMembershipForSave>
                {
                    new RoleMembershipForSave
                    {
                        EntityState = "Inserted",
                        RoleId      = 9999, // non existent Id
                        Memo        = "Nice"
                    }
                }
            };

            // Call the API
            var response = await _client.PostAsJsonAsync(localUsersURL, new List <LocalUserForSave> {
                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(localUsersURL, new List <LocalUserForSave> {
                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("is not activated", message.ToLower());
            }
        }