mirror of https://github.com/abpframework/abp.git
35 changed files with 460 additions and 58 deletions
@ -0,0 +1,32 @@ |
|||||
|
using System; |
||||
|
using System.Data; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Volo.Abp.Data; |
||||
|
using Volo.Abp.Uow; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling; |
||||
|
|
||||
|
public class ExceptionHandingUnitOfWork : UnitOfWork |
||||
|
{ |
||||
|
public ExceptionHandingUnitOfWork( |
||||
|
IServiceProvider serviceProvider, |
||||
|
IUnitOfWorkEventPublisher unitOfWorkEventPublisher, |
||||
|
IOptions<AbpUnitOfWorkDefaultOptions> options) |
||||
|
: base(serviceProvider, unitOfWorkEventPublisher, options) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
public async override Task SaveChangesAsync(CancellationToken cancellationToken = default) |
||||
|
{ |
||||
|
if (ServiceProvider.GetRequiredService<ICurrentUser>().Id == Guid.Empty) |
||||
|
{ |
||||
|
throw new AbpDbConcurrencyException(); |
||||
|
} |
||||
|
|
||||
|
await base.SaveChangesAsync(cancellationToken); |
||||
|
} |
||||
|
} |
||||
@ -1,17 +1,10 @@ |
|||||
@inject NavigationManager Navigation |
@inject NavigationManager Navigation |
||||
@inject IJSRuntime JSRuntime |
|
||||
|
|
||||
@code { |
@code { |
||||
|
|
||||
protected override void OnInitialized() |
protected override void OnInitialized() |
||||
{ |
{ |
||||
bool isWebAssembly = JSRuntime is IJSInProcessRuntime; |
Navigation.NavigateTo($"account/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}", true); |
||||
if (isWebAssembly) |
} |
||||
{ |
|
||||
Navigation.NavigateTo($"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}"); |
} |
||||
} |
|
||||
else |
|
||||
{ |
|
||||
Navigation.NavigateTo($"account/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}", true); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,17 @@ |
|||||
|
@inject NavigationManager Navigation |
||||
|
|
||||
|
@using Volo.Abp.DependencyInjection |
||||
|
@using Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic |
||||
|
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication |
||||
|
@inherits RedirectToLogin |
||||
|
@attribute [ExposeServices(typeof(RedirectToLogin))] |
||||
|
@attribute [Dependency(ReplaceServices = true)] |
||||
|
|
||||
|
@code { |
||||
|
|
||||
|
protected override void OnInitialized() |
||||
|
{ |
||||
|
Navigation.NavigateToLogin("authentication/login"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -1,2 +1,3 @@ |
|||||
export * from './config-options.token'; |
export * from './config-options.token'; |
||||
|
export * from './re-login-confirmation.token'; |
||||
export * from './extensions.token'; |
export * from './extensions.token'; |
||||
|
|||||
@ -0,0 +1,5 @@ |
|||||
|
import { InjectionToken } from '@angular/core'; |
||||
|
|
||||
|
export const RE_LOGIN_CONFIRMATION_TOKEN = new InjectionToken<boolean>( |
||||
|
'RE_LOGIN_CONFIRMATION_TOKEN', |
||||
|
); |
||||
@ -0,0 +1,62 @@ |
|||||
|
import { CoreTestingModule } from '@abp/ng.core/testing'; |
||||
|
import { ThemeBasicTestingModule } from '@abp/ng.theme.basic/testing'; |
||||
|
import { ThemeSharedTestingModule } from '@abp/ng.theme.shared/testing'; |
||||
|
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
||||
|
import { NgxValidateCoreModule } from '@ngx-validate/core'; |
||||
|
import { HomeComponent } from './home.component'; |
||||
|
import { OAuthService } from 'angular-oauth2-oidc'; |
||||
|
import { AuthService } from '@abp/ng.core'; |
||||
|
|
||||
|
describe('HomeComponent', () => { |
||||
|
let fixture: ComponentFixture<HomeComponent>; |
||||
|
const mockOAuthService = jasmine.createSpyObj('OAuthService', ['hasValidAccessToken']); |
||||
|
const mockAuthService = jasmine.createSpyObj('AuthService', ['navigateToLogin']); |
||||
|
beforeEach(waitForAsync(() => { |
||||
|
TestBed.configureTestingModule({ |
||||
|
declarations: [HomeComponent], |
||||
|
imports: [ |
||||
|
CoreTestingModule.withConfig(), |
||||
|
ThemeSharedTestingModule.withConfig(), |
||||
|
ThemeBasicTestingModule.withConfig(), |
||||
|
NgxValidateCoreModule, |
||||
|
], |
||||
|
providers: [ |
||||
|
/* mock providers here */ |
||||
|
{ |
||||
|
provide: OAuthService, |
||||
|
useValue: mockOAuthService, |
||||
|
}, |
||||
|
{ |
||||
|
provide: AuthService, |
||||
|
useValue: mockAuthService, |
||||
|
}, |
||||
|
], |
||||
|
}).compileComponents(); |
||||
|
})); |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
fixture = TestBed.createComponent(HomeComponent); |
||||
|
fixture.detectChanges(); |
||||
|
}); |
||||
|
|
||||
|
it('should be initiated', () => { |
||||
|
expect(fixture.componentInstance).toBeTruthy(); |
||||
|
}); |
||||
|
|
||||
|
describe('when login state is true', () => { |
||||
|
beforeAll(() => { |
||||
|
mockOAuthService.hasValidAccessToken.and.returnValue(true); |
||||
|
}); |
||||
|
|
||||
|
it('hasLoggedIn should be true', () => { |
||||
|
expect(fixture.componentInstance.hasLoggedIn).toBeTrue(); |
||||
|
expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled(); |
||||
|
}); |
||||
|
|
||||
|
it('button should not be exists', () => { |
||||
|
const element = fixture.nativeElement; |
||||
|
const cardTitle = element.querySelector('.card-title'); |
||||
|
expect(cardTitle).toBeTruthy(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,100 @@ |
|||||
|
import { CoreTestingModule } from "@abp/ng.core/testing"; |
||||
|
import { ThemeBasicTestingModule } from "@abp/ng.theme.basic/testing"; |
||||
|
import { ThemeSharedTestingModule } from "@abp/ng.theme.shared/testing"; |
||||
|
import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing"; |
||||
|
import { NgxValidateCoreModule } from "@ngx-validate/core"; |
||||
|
import { HomeComponent } from "./home.component"; |
||||
|
import { OAuthService } from 'angular-oauth2-oidc'; |
||||
|
import { AuthService } from '@abp/ng.core'; |
||||
|
|
||||
|
|
||||
|
describe("HomeComponent", () => { |
||||
|
let fixture: ComponentFixture<HomeComponent>; |
||||
|
const mockOAuthService = jasmine.createSpyObj('OAuthService', ['hasValidAccessToken']) |
||||
|
const mockAuthService = jasmine.createSpyObj('AuthService', ['navigateToLogin']) |
||||
|
beforeEach( |
||||
|
waitForAsync(() => { |
||||
|
TestBed.configureTestingModule({ |
||||
|
declarations: [HomeComponent], |
||||
|
imports: [ |
||||
|
CoreTestingModule.withConfig(), |
||||
|
ThemeSharedTestingModule.withConfig(), |
||||
|
ThemeBasicTestingModule.withConfig(), |
||||
|
NgxValidateCoreModule, |
||||
|
], |
||||
|
providers: [ |
||||
|
/* mock providers here */ |
||||
|
{ |
||||
|
provide: OAuthService, |
||||
|
useValue: mockOAuthService |
||||
|
}, |
||||
|
{ |
||||
|
provide: AuthService, |
||||
|
useValue: mockAuthService |
||||
|
} |
||||
|
], |
||||
|
}).compileComponents(); |
||||
|
}) |
||||
|
); |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
fixture = TestBed.createComponent(HomeComponent); |
||||
|
fixture.detectChanges(); |
||||
|
}); |
||||
|
|
||||
|
it("should be initiated", () => { |
||||
|
expect(fixture.componentInstance).toBeTruthy(); |
||||
|
}); |
||||
|
|
||||
|
|
||||
|
|
||||
|
describe('when login state is true', () => { |
||||
|
beforeAll(() => { |
||||
|
mockOAuthService.hasValidAccessToken.and.returnValue(true) |
||||
|
}); |
||||
|
|
||||
|
it("hasLoggedIn should be true", () => { |
||||
|
|
||||
|
expect(fixture.componentInstance.hasLoggedIn).toBeTrue(); |
||||
|
expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled() |
||||
|
}) |
||||
|
|
||||
|
it("button should not be exists", () => { |
||||
|
const element = fixture.nativeElement |
||||
|
const button = element.querySelector('[role="button"]') |
||||
|
expect(button).toBeNull() |
||||
|
}) |
||||
|
|
||||
|
}) |
||||
|
|
||||
|
describe('when login state is false', () => { |
||||
|
beforeAll(() => { |
||||
|
mockOAuthService.hasValidAccessToken.and.returnValue(false) |
||||
|
}); |
||||
|
|
||||
|
it("hasLoggedIn should be false", () => { |
||||
|
|
||||
|
expect(fixture.componentInstance.hasLoggedIn).toBeFalse(); |
||||
|
expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled() |
||||
|
}) |
||||
|
|
||||
|
it("button should be exists", () => { |
||||
|
const element = fixture.nativeElement |
||||
|
const button = element.querySelector('[role="button"]') |
||||
|
expect(button).toBeDefined() |
||||
|
}) |
||||
|
describe('when button clicked', () => { |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
const element = fixture.nativeElement |
||||
|
const button = element.querySelector('[role="button"]') |
||||
|
button.click() |
||||
|
}); |
||||
|
|
||||
|
it("navigateToLogin have been called", () => { |
||||
|
expect(mockAuthService.navigateToLogin).toHaveBeenCalled() |
||||
|
}) |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
}); |
||||
Loading…
Reference in new issue