-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.spec.ts
181 lines (132 loc) · 5.46 KB
/
file.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {TestBed, fakeAsync, inject, tick} from "@angular/core/testing";
import {MockBackend, MockConnection} from "@angular/http/testing";
import {BaseRequestOptions, Http, HttpModule, RequestMethod, ResponseOptions, Response, Headers} from "@angular/http";
import {TESTCONFIG, TESTURL, mockUploadFile, mockExistingFile} from "./helpers";
import {SelfbitsFile} from "../src/services/file";
import {SELFBITS_CONFIG} from "../src/utils/tokens";
describe('file.ts',()=> {
beforeEach(()=> {
TestBed.configureTestingModule({
providers: [
MockBackend,
BaseRequestOptions,
{
provide: Http, useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
{ provide: SELFBITS_CONFIG, useValue:TESTCONFIG},
SelfbitsFile
],
imports: [
HttpModule
]
})
});
afterEach(()=>{
window.localStorage.removeItem('token');
window.localStorage.removeItem('userId');
window.localStorage.removeItem('expires');
});
it('should load with config data injected',inject([SelfbitsFile],(file:any)=>{
expect(file.config).toEqual(TESTCONFIG);
expect(file.headers.get('sb-app-id')).toEqual('fancyId');
expect(file.headers.get('sb-app-secret')).toEqual('fancySecret');
expect(file.headers.get('Content-Type')).toEqual('application/json');
expect(file.baseUrl).toEqual(TESTURL);
}));
it('checkForToken() should append Authorization header ', inject([SelfbitsFile],(file:any)=>{
window.localStorage.setItem('token','httpTestToken');
expect(file.headers.has('Authorization')).toBeFalsy();
file.checkForToken();
expect(file.headers.get('Authorization')).toEqual('Bearer httpTestToken');
}));
describe('file http methods', ()=>{
let file:SelfbitsFile;
let backend:MockBackend;
let checkTokenSpy:any;
let response:any;
beforeEach(inject([SelfbitsFile,MockBackend],(testFile:SelfbitsFile,testBackend:MockBackend)=>{
file = testFile;
backend = testBackend;
checkTokenSpy = spyOn(testFile,'checkForToken');
}));
afterEach(()=>{
file = null;
backend = null;
checkTokenSpy = null;
response = null;
});
it('should have a working getAll()',fakeAsync(()=>{
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Get);
expect(connection.request.url).toBe(`${TESTURL}/api/v1/file`);
let mockResponseBody = { content: 'Testing' };
let response = new ResponseOptions({ body: JSON.stringify(mockResponseBody) });
connection.mockRespond(new Response(response));
});
file.getAll().subscribe(res => {
response = res.json();
});
expect(checkTokenSpy).toHaveBeenCalled();
expect(response.content).toBe('Testing');
}));
it('should have a working upload()',fakeAsync(()=>{
let mockInitialUploadResponse = { responseName:"initiateUpload", putFileUrl: 'amazonPutUrl', fileId: 'd411ac6'};
let mockExecuteUploadResponse = { responseName:"executeUpload", putFileUrl: 'executeUploadUploadResponse'};
let mockVerifyUploadResponse = { responseName:"verifyUpload"};
backend.connections.subscribe((connection: MockConnection) => {
/* Testing initiateUpload */
if(connection.request['url']=="fancyUrl/api/v1/file"){
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.getBody()).toBe(
JSON.stringify({"filePath":"the/File/Path","permissionScope":"user"})
);
let response = new ResponseOptions({ body: JSON.stringify(mockInitialUploadResponse) });
connection.mockRespond(new Response(response));
}
/* Testing executeUpload*/
else if(connection.request['url'] === "amazonPutUrl"){
expect(connection.request.method).toBe(RequestMethod.Put);
expect(connection.request.getBody()).toBe(JSON.stringify("contentOfTheFile"));
let amazonMockHeader = new Headers();
amazonMockHeader.append('ETag', 'theMockETag');
let response = new ResponseOptions({
body: JSON.stringify(mockExecuteUploadResponse),
headers: amazonMockHeader
});
connection.mockRespond(new Response(response));
}
/* Testing verifyUpload */
else if(connection.request['url'] === "fancyUrl/api/v1/file/d411ac6/verify"){
expect(connection.request.method).toBe(RequestMethod.Post);
expect(connection.request.getBody()).toBe(JSON.stringify({"etag":"theMockETag"}));
let response = new ResponseOptions({ body: JSON.stringify(mockVerifyUploadResponse) });
connection.mockRespond(new Response(response));
}
});
file.upload(mockUploadFile).subscribe(res => {
expect(res.json().responseName).toBe("verifyUpload");
});
tick();
expect(checkTokenSpy).toHaveBeenCalled()
}));
it('should have a working get()',fakeAsync(()=>{
backend.connections.subscribe((connection: MockConnection) => {
expect(connection.request.method).toBe(RequestMethod.Get);
let requestPath = `${TESTURL}/api/v1/file/`+mockExistingFile.fileId
+'?expiresInSeconds='+mockExistingFile.expiresInSeconds;
expect(connection.request.url).toBe(requestPath);
let mockResponseBody = { content: 'Testing' };
let response = new ResponseOptions({ body: JSON.stringify(mockResponseBody) });
connection.mockRespond(new Response(response));
});
file.get(mockExistingFile).subscribe(res => {
response = res.json();
});
tick();
expect(response.content).toBe('Testing');
expect(checkTokenSpy).toHaveBeenCalled()
}));
})
})