@@ -7,56 +7,155 @@ import { TestScheduler } from 'rxjs/testing';
77import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils' ;
88import { ShortLivedToken } from './models/short-lived-token.model' ;
99import { RemoteData } from '../data/remote-data' ;
10+ import { HttpOptions } from '../dspace-rest/dspace-rest.service' ;
11+ import objectContaining = jasmine . objectContaining ;
12+ import { AuthStatus } from './models/auth-status.model' ;
13+ import { RestRequestMethod } from '../data/rest-request-method' ;
1014
1115describe ( `AuthRequestService` , ( ) => {
1216 let halService : HALEndpointService ;
1317 let endpointURL : string ;
18+ let requestID : string ;
1419 let shortLivedToken : ShortLivedToken ;
1520 let shortLivedTokenRD : RemoteData < ShortLivedToken > ;
1621 let requestService : RequestService ;
1722 let rdbService : RemoteDataBuildService ;
18- let service : AuthRequestService ;
23+ let service ;
1924 let testScheduler ;
2025
21- class TestAuthRequestService extends AuthRequestService {
22- constructor (
23- hes : HALEndpointService ,
24- rs : RequestService ,
25- rdbs : RemoteDataBuildService
26- ) {
27- super ( hes , rs , rdbs ) ;
28- }
29-
30- protected createShortLivedTokenRequest ( href : string ) : PostRequest {
31- return new PostRequest ( this . requestService . generateRequestId ( ) , href ) ;
32- }
26+ const status = new AuthStatus ( ) ;
27+
28+ class TestAuthRequestService extends AuthRequestService {
29+ constructor (
30+ hes : HALEndpointService ,
31+ rs : RequestService ,
32+ rdbs : RemoteDataBuildService
33+ ) {
34+ super ( hes , rs , rdbs ) ;
3335 }
3436
35- const init = ( cold : typeof TestScheduler . prototype . createColdObservable ) => {
36- endpointURL = 'https://rest.api/auth' ;
37- shortLivedToken = Object . assign ( new ShortLivedToken ( ) , {
38- value : 'some-token'
39- } ) ;
40- shortLivedTokenRD = createSuccessfulRemoteDataObject ( shortLivedToken ) ;
37+ protected createShortLivedTokenRequest ( href : string ) : PostRequest {
38+ return new PostRequest ( this . requestService . generateRequestId ( ) , href ) ;
39+ }
40+ }
41+
42+ const init = ( cold : typeof TestScheduler . prototype . createColdObservable ) => {
43+ endpointURL = 'https://rest.api/auth' ;
44+ requestID = 'requestID' ;
45+ shortLivedToken = Object . assign ( new ShortLivedToken ( ) , {
46+ value : 'some-token'
47+ } ) ;
48+ shortLivedTokenRD = createSuccessfulRemoteDataObject ( shortLivedToken ) ;
49+
50+ halService = jasmine . createSpyObj ( 'halService' , {
51+ 'getEndpoint' : cold ( 'a' , { a : endpointURL } )
52+ } ) ;
53+ requestService = jasmine . createSpyObj ( 'requestService' , {
54+ 'generateRequestId' : requestID ,
55+ 'send' : null ,
56+ } ) ;
57+ rdbService = jasmine . createSpyObj ( 'rdbService' , {
58+ 'buildFromRequestUUID' : cold ( 'a' , { a : shortLivedTokenRD } )
59+ } ) ;
60+
61+ service = new TestAuthRequestService ( halService , requestService , rdbService ) ;
62+
63+ spyOn ( service as any , 'fetchRequest' ) . and . returnValue ( cold ( 'a' , { a : createSuccessfulRemoteDataObject ( status ) } ) ) ;
64+ } ;
65+
66+ beforeEach ( ( ) => {
67+ testScheduler = new TestScheduler ( ( actual , expected ) => {
68+ expect ( actual ) . toEqual ( expected ) ;
69+ } ) ;
70+ } ) ;
71+
72+ describe ( 'REST request methods' , ( ) => {
73+ let options : HttpOptions ;
4174
42- halService = jasmine . createSpyObj ( 'halService' , {
43- 'getEndpoint' : cold ( 'a' , { a : endpointURL } )
75+ beforeEach ( ( ) => {
76+ options = Object . create ( { } ) ;
77+ } ) ;
78+
79+ describe ( 'GET' , ( ) => {
80+ it ( 'should send a GET request to the right endpoint and return the auth status' , ( ) => {
81+ testScheduler . run ( ( { cold, expectObservable, flush } ) => {
82+ init ( cold ) ;
83+
84+ expectObservable ( service . getRequest ( 'method' , options ) ) . toBe ( 'a' , {
85+ a : objectContaining ( { payload : status } ) ,
86+ } ) ;
87+ flush ( ) ;
88+
89+ expect ( requestService . send ) . toHaveBeenCalledWith ( objectContaining ( {
90+ uuid : requestID ,
91+ href : endpointURL + '/method' ,
92+ method : RestRequestMethod . GET ,
93+ body : undefined ,
94+ options,
95+ } ) ) ;
96+ expect ( ( service as any ) . fetchRequest ) . toHaveBeenCalledWith ( requestID ) ;
97+ } ) ;
4498 } ) ;
45- requestService = jasmine . createSpyObj ( 'requestService' , {
46- 'send' : null
99+
100+ it ( 'should send the request even if caller doesn\'t subscribe to the response' , ( ) => {
101+ testScheduler . run ( ( { cold, flush } ) => {
102+ init ( cold ) ;
103+
104+ service . getRequest ( 'method' , options ) ;
105+ flush ( ) ;
106+
107+ expect ( requestService . send ) . toHaveBeenCalledWith ( objectContaining ( {
108+ uuid : requestID ,
109+ href : endpointURL + '/method' ,
110+ method : RestRequestMethod . GET ,
111+ body : undefined ,
112+ options,
113+ } ) ) ;
114+ expect ( ( service as any ) . fetchRequest ) . toHaveBeenCalledWith ( requestID ) ;
115+ } ) ;
47116 } ) ;
48- rdbService = jasmine . createSpyObj ( 'rdbService' , {
49- 'buildFromRequestUUID' : cold ( 'a' , { a : shortLivedTokenRD } )
117+ } ) ;
118+
119+ describe ( 'POST' , ( ) => {
120+ it ( 'should send a POST request to the right endpoint and return the auth status' , ( ) => {
121+ testScheduler . run ( ( { cold, expectObservable, flush } ) => {
122+ init ( cold ) ;
123+
124+ expectObservable ( service . postToEndpoint ( 'method' , { content : 'something' } , options ) ) . toBe ( 'a' , {
125+ a : objectContaining ( { payload : status } ) ,
126+ } ) ;
127+ flush ( ) ;
128+
129+ expect ( requestService . send ) . toHaveBeenCalledWith ( objectContaining ( {
130+ uuid : requestID ,
131+ href : endpointURL + '/method' ,
132+ method : RestRequestMethod . POST ,
133+ body : { content : 'something' } ,
134+ options,
135+ } ) ) ;
136+ expect ( ( service as any ) . fetchRequest ) . toHaveBeenCalledWith ( requestID ) ;
137+ } ) ;
50138 } ) ;
51139
52- service = new TestAuthRequestService ( halService , requestService , rdbService ) ;
53- } ;
140+ it ( 'should send the request even if caller doesn\'t subscribe to the response' , ( ) => {
141+ testScheduler . run ( ( { cold, flush } ) => {
142+ init ( cold ) ;
54143
55- beforeEach ( ( ) => {
56- testScheduler = new TestScheduler ( ( actual , expected ) => {
57- expect ( actual ) . toEqual ( expected ) ;
144+ service . postToEndpoint ( 'method' , { content : 'something' } , options ) ;
145+ flush ( ) ;
146+
147+ expect ( requestService . send ) . toHaveBeenCalledWith ( objectContaining ( {
148+ uuid : requestID ,
149+ href : endpointURL + '/method' ,
150+ method : RestRequestMethod . POST ,
151+ body : { content : 'something' } ,
152+ options,
153+ } ) ) ;
154+ expect ( ( service as any ) . fetchRequest ) . toHaveBeenCalledWith ( requestID ) ;
155+ } ) ;
58156 } ) ;
59157 } ) ;
158+ } ) ;
60159
61160 describe ( `getShortlivedToken` , ( ) => {
62161 it ( `should call createShortLivedTokenRequest with the url for the endpoint` , ( ) => {
0 commit comments