public void Register_WithAlreadyExistingUsername_ThrowsUsernameAlreadyExistsException()
        {
            string username = "******";

            _mockUnitOfWork.Setup(s => s.UserRepository.GetByUsername(username)).ReturnsAsync(new User());

            UsernameAlreadyExistsException exception = Assert.ThrowsAsync <UsernameAlreadyExistsException>(
                () => _authenticationService.Register(username, It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()));

            string actualUsername = exception.Username;

            Assert.AreEqual(username, actualUsername);
        }
示例#2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            /*
             * if (env.IsDevelopment())
             * {
             *  app.UseDeveloperExceptionPage();
             * }
             * else
             * {
             *  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
             *  app.UseHsts();
             * }
             */
            app.UseExceptionHandler(errorApp =>
            {
                errorApp.Run(async context =>
                {
                    var exceptionHandlerPathFeature =
                        context.Features.Get <IExceptionHandlerPathFeature>();

                    string text = exceptionHandlerPathFeature?.Error switch
                    {
                        UserNotFoundException e => "User not found",
                        UserNotHaveEnoughRightsException e => "User not have enough rights",
                        UserAlreadyInChatException e => "User already in chat",
                        ChatNotFoundException e => "Chat not found",
                        UsernameAlreadyExistsException e => "Username already exists",
                        _ => "Unknown error"
                    };
                    context.Response.ContentType = MediaTypeNames.Application.Json;
                    context.Response.StatusCode  = 400;
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(new ApiError {
                        Error = text
                    }));
                });
            });

            app.UseOpenApi(settings =>
            {
                settings.Path         = $"{ApiConstant.Prefix}openapi/swagger.json";
                settings.DocumentName = "openapi";
            });

            app.UseSwaggerUi3(options =>
            {
                options.Path         = $"{ApiConstant.Prefix}openapi";
                options.DocumentPath = $"{ApiConstant.Prefix}openapi/swagger.json";
            });

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }