mirror of https://github.com/Squidex/squidex.git
10 changed files with 334 additions and 8 deletions
@ -0,0 +1,89 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
|
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { |
||||
|
AuthService, |
||||
|
Profile, |
||||
|
User, |
||||
|
UsersProviderService, |
||||
|
UsersService, |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('UsersProviderService', () => { |
||||
|
let authService: TypeMoq.Mock<AuthService>; |
||||
|
let usersService: TypeMoq.Mock<UsersService>; |
||||
|
let usersProviderService: UsersProviderService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = TypeMoq.Mock.ofType(AuthService); |
||||
|
usersService = TypeMoq.Mock.ofType(UsersService); |
||||
|
usersProviderService = new UsersProviderService(usersService.object, authService.object); |
||||
|
}); |
||||
|
|
||||
|
it('Should return users service when user not cached', () => { |
||||
|
const user = new User('123', 'path/to/image', 'mail@domain.com'); |
||||
|
|
||||
|
usersService.setup(x => x.getUser('123')) |
||||
|
.returns(() => Observable.of(user)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let resultingUser: User = null; |
||||
|
|
||||
|
usersProviderService.getUser('123').subscribe(result => { |
||||
|
resultingUser = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(resultingUser).toBe(user); |
||||
|
|
||||
|
usersService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('Should return provide user from cache', () => { |
||||
|
const user = new User('123', 'path/to/image', 'mail@domain.com'); |
||||
|
|
||||
|
usersService.setup(x => x.getUser('123')) |
||||
|
.returns(() => Observable.of(user)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
usersProviderService.getUser('123'); |
||||
|
|
||||
|
let resultingUser: User = null; |
||||
|
|
||||
|
usersProviderService.getUser('123').subscribe(result => { |
||||
|
resultingUser = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(resultingUser).toBe(user); |
||||
|
|
||||
|
usersService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('Should return Me when user is current user', () => { |
||||
|
const user = new User('123', 'path/to/image', 'mail@domain.com'); |
||||
|
|
||||
|
authService.setup(x => x.user) |
||||
|
.returns(() => new Profile(<any>{ profile: { sub: '123'}})); |
||||
|
|
||||
|
usersService.setup(x => x.getUser('123')) |
||||
|
.returns(() => Observable.of(user)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let resultingUser: User = null; |
||||
|
|
||||
|
usersProviderService.getUser('123').subscribe(result => { |
||||
|
resultingUser = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(resultingUser).toEqual(new User('123', 'path/to/image', 'Me')); |
||||
|
|
||||
|
usersService.verifyAll(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,44 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as Ng2 from '@angular/core'; |
||||
|
|
||||
|
import { Observable, } from 'rxjs'; |
||||
|
|
||||
|
import { User, UsersService } from './users.service'; |
||||
|
|
||||
|
import { AuthService } from './auth.service'; |
||||
|
|
||||
|
@Ng2.Injectable() |
||||
|
export class UsersProviderService { |
||||
|
private readonly caches: { [id: string]: Observable<User> } = {}; |
||||
|
|
||||
|
constructor( |
||||
|
private readonly usersService: UsersService, |
||||
|
private readonly authService: AuthService, |
||||
|
) { |
||||
|
} |
||||
|
|
||||
|
public getUser(id: string): Observable<User> { |
||||
|
let result = this.caches[id]; |
||||
|
|
||||
|
if (!result) { |
||||
|
result = this.caches[id] = |
||||
|
this.usersService.getUser(id) |
||||
|
.map(u => { |
||||
|
if (this.authService.user && u.id === this.authService.user.id) { |
||||
|
return new User(u.id, u.profileUrl, 'Me'); |
||||
|
} else { |
||||
|
return u; |
||||
|
} |
||||
|
}) |
||||
|
.publishLast().refCount(); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,122 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
import * as Ng2Http from '@angular/http'; |
||||
|
|
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { |
||||
|
ApiUrlConfig, |
||||
|
AuthService, |
||||
|
User, |
||||
|
UsersService, |
||||
|
} from './../'; |
||||
|
|
||||
|
describe('UsersService', () => { |
||||
|
let authService: TypeMoq.Mock<AuthService>; |
||||
|
let usersService: UsersService; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = TypeMoq.Mock.ofType(AuthService); |
||||
|
usersService = new UsersService(authService.object, new ApiUrlConfig('http://service/p/')); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request with auth service to get many users', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/users/?query=')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions({ |
||||
|
body: [{ |
||||
|
id: '123', |
||||
|
profileUrl: 'path/to/image1', |
||||
|
displayName: 'mail1@domain.com' |
||||
|
}, { |
||||
|
id: '456', |
||||
|
profileUrl: 'path/to/image2', |
||||
|
displayName: 'mail2@domain.com' |
||||
|
}] |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let user: User[] = null; |
||||
|
|
||||
|
usersService.getUsers().subscribe(result => { |
||||
|
user = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(user).toEqual( |
||||
|
[ |
||||
|
new User('123', 'path/to/image1', 'mail1@domain.com'), |
||||
|
new User('456', 'path/to/image2', 'mail2@domain.com') |
||||
|
]); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request with auth service and query to get many users', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/users/?query=my-query')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions({ |
||||
|
body: [{ |
||||
|
id: '123', |
||||
|
profileUrl: 'path/to/image1', |
||||
|
displayName: 'mail1@domain.com' |
||||
|
}, { |
||||
|
id: '456', |
||||
|
profileUrl: 'path/to/image2', |
||||
|
displayName: 'mail2@domain.com' |
||||
|
}] |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let user: User[] = null; |
||||
|
|
||||
|
usersService.getUsers('my-query').subscribe(result => { |
||||
|
user = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(user).toEqual( |
||||
|
[ |
||||
|
new User('123', 'path/to/image1', 'mail1@domain.com'), |
||||
|
new User('456', 'path/to/image2', 'mail2@domain.com') |
||||
|
]); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
|
||||
|
it('should make get request with auth service to get single user', () => { |
||||
|
authService.setup(x => x.authGet('http://service/p/api/users/123')) |
||||
|
.returns(() => Observable.of( |
||||
|
new Ng2Http.Response( |
||||
|
new Ng2Http.ResponseOptions({ |
||||
|
body: { |
||||
|
id: '123', |
||||
|
profileUrl: 'path/to/image', |
||||
|
displayName: 'mail@domain.com' |
||||
|
} |
||||
|
}) |
||||
|
) |
||||
|
)) |
||||
|
.verifiable(TypeMoq.Times.once()); |
||||
|
|
||||
|
let user: User = null; |
||||
|
|
||||
|
usersService.getUser('123').subscribe(result => { |
||||
|
user = result; |
||||
|
}).unsubscribe(); |
||||
|
|
||||
|
expect(user).toEqual(new User('123', 'path/to/image', 'mail@domain.com')); |
||||
|
|
||||
|
authService.verifyAll(); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,58 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as Ng2 from '@angular/core'; |
||||
|
|
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import { ApiUrlConfig } from 'framework'; |
||||
|
|
||||
|
import { AuthService } from './auth.service'; |
||||
|
|
||||
|
export class User { |
||||
|
constructor( |
||||
|
public readonly id: string, |
||||
|
public readonly profileUrl: string, |
||||
|
public readonly displayName: string |
||||
|
) { |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Ng2.Injectable() |
||||
|
export class UsersService { |
||||
|
constructor( |
||||
|
private readonly authService: AuthService, |
||||
|
private readonly apiUrl: ApiUrlConfig |
||||
|
) { |
||||
|
} |
||||
|
|
||||
|
public getUsers(query?: string): Observable<User[]> { |
||||
|
return this.authService.authGet(this.apiUrl.buildUrl(`api/users/?query=${query || ''}`)) |
||||
|
.map(response => { |
||||
|
const body: any[] = response.json() || []; |
||||
|
|
||||
|
return body.map(item => { |
||||
|
return new User( |
||||
|
item.id, |
||||
|
item.profileUrl, |
||||
|
item.displayName); |
||||
|
}); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public getUser(id: string): Observable<User> { |
||||
|
return this.authService.authGet(this.apiUrl.buildUrl(`api/users/${id}`)) |
||||
|
.map(response => { |
||||
|
const body: any = response.json(); |
||||
|
|
||||
|
return new User( |
||||
|
body.id, |
||||
|
body.profileUrl, |
||||
|
body.displayName); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
{ |
||||
|
"configProperties": { |
||||
|
"System.GC.Server": true |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue