Ejemplo n.º 1
0
        public async void CreateUserWithInvalidDateofbirthTest()
        {
            // invalid date of birth
            var jsonDictInvalid = new Dictionary <string, object> {
                { "name", "Pete" },
                { "email", "*****@*****.**" },
                { "password", "10290292034" },
                { "dateofbirth", "invaliddateofbirth" },
                { "gender", 0 },
                { "type", 0 }
            };
            var          jsonInvalid    = JsonConvert.SerializeObject(jsonDictInvalid, Formatting.Indented);
            HttpRequest  requestInvalid = HttpRequestFactory.CreatePostRequest(jsonInvalid);
            ObjectResult resultInvalid  = (ObjectResult)await this.userController.UserCreate(requestInvalid, this.adminClaim);

            // date of birth in the future
            User userFuture = UserFactory.CreateNormalUser();

            userFuture.Dateofbirth = DateTime.Now.AddYears(1);
            HttpRequest  requestFuture = HttpRequestFactory.CreatePostRequest <User>(userFuture);
            ObjectResult resultFuture  = (ObjectResult)await this.userController.UserCreate(requestFuture, this.adminClaim);

            // status code should be 400 BAD REQUEST
            Assert.Equal(400, resultInvalid.StatusCode);
            Assert.Equal(400, resultFuture.StatusCode);
        }
Ejemplo n.º 2
0
        public async void CreateNewUsersAsAdminTest()
        {
            HttpRequest request = HttpRequestFactory.CreatePostRequest <User>(UserFactory.CreateNormalUser());

            ObjectResult result = (ObjectResult)await this.userController.UserCreate(request, this.adminClaim);

            // status code should be 200 OK
            Assert.Equal(200, result.StatusCode);
            // database should contain 1 user
            Assert.Single(await this.userRepository.ListAsync());
        }
        private async Task <JwtTokenDto> LoggingIn(CancellationToken cancellationToken = default)
        {
            var url = ApiEndpoints.CreateAuthToken();

            var requestBody = new CredentialsDto {
                Email = "*****@*****.**", Password = "******"
            }.ToStringContent();

            var requestMessage = _httpRequestFactory.CreatePostRequest(url, requestBody);

            return(await _httpClient.ExecuteRequest <JwtTokenDto>(requestMessage, cancellationToken));
        }
Ejemplo n.º 4
0
        public async void CreateUserWithInvalidPasswordTest()
        {
            // too short password
            var jsonDictShort = new Dictionary <string, object> {
                { "name", "Pete" },
                { "email", "*****@*****.**" },
                { "password", "1234" },
                { "dateofbirth", "2000-01-01" },
                { "gender", 0 }
            };
            var          jsonShort    = JsonConvert.SerializeObject(jsonDictShort, Formatting.Indented);
            HttpRequest  requestShort = HttpRequestFactory.CreatePostRequest(jsonShort);
            ObjectResult resultShort  = (ObjectResult)await this.userController.UserCreate(requestShort, this.adminClaim);

            // status code should be 400 BAD REQUEST
            Assert.Equal(400, resultShort.StatusCode);
        }
Ejemplo n.º 5
0
        public async void CreateUserWithoutTypeFieldTest()
        {
            // create json
            var jsonDict = new Dictionary <string, object> {
                { "name", "Pete" },
                { "email", "*****@*****.**" },
                { "password", "10290292034" },
                { "dateofbirth", "2000-01-01" },
                { "gender", 0 }
            };
            var json = JsonConvert.SerializeObject(jsonDict, Formatting.Indented);

            // create request
            HttpRequest request = HttpRequestFactory.CreatePostRequest(json);

            ObjectResult result = (ObjectResult)await this.userController.UserCreate(request, this.adminClaim);

            // status code should be 400 OK
            Assert.Equal(400, result.StatusCode);
        }
Ejemplo n.º 6
0
        public async void CreateUserWithInvalidNameTest()
        {
            // too long name
            User userLong = UserFactory.CreateNormalUser();

            userLong.Name = "toolongname000000000000000000000000000000000";
            HttpRequest  requestLong = HttpRequestFactory.CreatePostRequest <User>(userLong);
            ObjectResult resultLong  = (ObjectResult)await this.userController.UserCreate(requestLong, this.adminClaim);

            // too short name
            User userShort = UserFactory.CreateNormalUser();

            userShort.Name = "0";
            HttpRequest  requestShort = HttpRequestFactory.CreatePostRequest <User>(userShort);
            ObjectResult resultShort  = (ObjectResult)await this.userController.UserCreate(requestShort, this.adminClaim);

            // status code should be 400 BAD REQUEST
            Assert.Equal(400, resultLong.StatusCode);
            Assert.Equal(400, resultShort.StatusCode);
        }
Ejemplo n.º 7
0
        public async void CreateNewUsersAsNonAdminTest()
        {
            HttpRequest requestAnonymous = HttpRequestFactory.CreatePostRequest <User>(UserFactory.CreateNormalUser(1, "*****@*****.**"));
            HttpRequest requestUser      = HttpRequestFactory.CreatePostRequest <User>(UserFactory.CreateNormalUser(2, "*****@*****.**"));
            HttpRequest requestOrganiser = HttpRequestFactory.CreatePostRequest <User>(UserFactory.CreateNormalUser(3, "*****@*****.**"));
            HttpRequest requestWithType  = HttpRequestFactory.CreatePostRequest <User>(UserFactory.CreateOrganiserUser());

            ObjectResult resultAnonymous = (ObjectResult)await this.userController.UserCreate(requestAnonymous, new ClaimsPrincipal());

            ObjectResult resultUser = (ObjectResult)await this.userController.UserCreate(requestUser, this.userClaim);

            ObjectResult resultOrganiser = (ObjectResult)await this.userController.UserCreate(requestOrganiser, this.organiserClaim);

            ObjectResult resultUserWithType = (ObjectResult)await this.userController.UserCreate(requestWithType, this.userClaim);

            UserResponse  resultUserAnonymous      = (UserResponse)resultAnonymous.Value;
            UserResponse  resultUserUser           = (UserResponse)resultUser.Value;
            UserResponse  resultUserOrganiser      = (UserResponse)resultOrganiser.Value;
            ErrorResponse errorMessageUserWithType = (ErrorResponse)resultUserWithType.Value;

            // status code should be 200 OK
            Assert.Equal(200, resultAnonymous.StatusCode);
            Assert.Equal(200, resultUser.StatusCode);
            Assert.Equal(200, resultOrganiser.StatusCode);
            // status code should be 403 FORBIDDEN
            Assert.Equal(403, resultUserWithType.StatusCode);

            // returned users should not be null
            Assert.NotNull(resultUserAnonymous);
            Assert.NotNull(resultUserUser);
            Assert.NotNull(resultUserOrganiser);
            // error code must be unauthorized because role has no permissions
            Assert.Equal(ErrorCode.UNAUTHORIZED_TO_SET_USER_TYPE, errorMessageUserWithType.ErrorCodeEnum);
            // database should contain 3 users
            Assert.Equal(3, (await this.userRepository.ListAsync()).Count);
        }