diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-09-04 21:21:47 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-09-04 21:30:18 +0200 |
commit | 0e1dc3e7c69995c691e1dd82e3c2bc68748661ca (patch) | |
tree | f2a4b5cffc72e33c902b67083bbaa35b6f22f0ca /server/tests/utils/users.ts | |
parent | b0f9f39ed70299a208d1b388c72de8b7f3510cb7 (diff) | |
download | PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.tar.gz PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.tar.zst PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.zip |
Convert tests to typescript
Diffstat (limited to 'server/tests/utils/users.ts')
-rw-r--r-- | server/tests/utils/users.ts | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/server/tests/utils/users.ts b/server/tests/utils/users.ts new file mode 100644 index 000000000..6e4b5f2f5 --- /dev/null +++ b/server/tests/utils/users.ts | |||
@@ -0,0 +1,115 @@ | |||
1 | import * as request from 'supertest' | ||
2 | |||
3 | function createUser (url: string, accessToken: string, username: string, password: string, specialStatus = 204) { | ||
4 | const path = '/api/v1/users' | ||
5 | const body = { | ||
6 | username, | ||
7 | password, | ||
8 | email: username + '@example.com' | ||
9 | } | ||
10 | |||
11 | return request(url) | ||
12 | .post(path) | ||
13 | .set('Accept', 'application/json') | ||
14 | .set('Authorization', 'Bearer ' + accessToken) | ||
15 | .send(body) | ||
16 | .expect(specialStatus) | ||
17 | } | ||
18 | |||
19 | function registerUser (url: string, username: string, password: string, specialStatus = 204) { | ||
20 | const path = '/api/v1/users/register' | ||
21 | const body = { | ||
22 | username, | ||
23 | password, | ||
24 | email: username + '@example.com' | ||
25 | } | ||
26 | |||
27 | return request(url) | ||
28 | .post(path) | ||
29 | .set('Accept', 'application/json') | ||
30 | .send(body) | ||
31 | .expect(specialStatus) | ||
32 | } | ||
33 | |||
34 | function getUserInformation (url: string, accessToken: string) { | ||
35 | const path = '/api/v1/users/me' | ||
36 | |||
37 | return request(url) | ||
38 | .get(path) | ||
39 | .set('Accept', 'application/json') | ||
40 | .set('Authorization', 'Bearer ' + accessToken) | ||
41 | .expect(200) | ||
42 | .expect('Content-Type', /json/) | ||
43 | } | ||
44 | |||
45 | function getUserVideoRating (url: string, accessToken: string, videoId: number) { | ||
46 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | ||
47 | |||
48 | return request(url) | ||
49 | .get(path) | ||
50 | .set('Accept', 'application/json') | ||
51 | .set('Authorization', 'Bearer ' + accessToken) | ||
52 | .expect(200) | ||
53 | .expect('Content-Type', /json/) | ||
54 | } | ||
55 | |||
56 | function getUsersList (url: string) { | ||
57 | const path = '/api/v1/users' | ||
58 | |||
59 | return request(url) | ||
60 | .get(path) | ||
61 | .set('Accept', 'application/json') | ||
62 | .expect(200) | ||
63 | .expect('Content-Type', /json/) | ||
64 | } | ||
65 | |||
66 | function getUsersListPaginationAndSort (url: string, start: number, count: number, sort: string) { | ||
67 | const path = '/api/v1/users' | ||
68 | |||
69 | return request(url) | ||
70 | .get(path) | ||
71 | .query({ start }) | ||
72 | .query({ count }) | ||
73 | .query({ sort }) | ||
74 | .set('Accept', 'application/json') | ||
75 | .expect(200) | ||
76 | .expect('Content-Type', /json/) | ||
77 | } | ||
78 | |||
79 | function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) { | ||
80 | const path = '/api/v1/users' | ||
81 | |||
82 | return request(url) | ||
83 | .delete(path + '/' + userId) | ||
84 | .set('Accept', 'application/json') | ||
85 | .set('Authorization', 'Bearer ' + accessToken) | ||
86 | .expect(expectedStatus) | ||
87 | } | ||
88 | |||
89 | function updateUser (url: string, userId: number, accessToken: string, newPassword: string, displayNSFW: boolean) { | ||
90 | const path = '/api/v1/users/' + userId | ||
91 | |||
92 | const toSend = {} | ||
93 | if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword | ||
94 | if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW | ||
95 | |||
96 | return request(url) | ||
97 | .put(path) | ||
98 | .set('Accept', 'application/json') | ||
99 | .set('Authorization', 'Bearer ' + accessToken) | ||
100 | .send(toSend) | ||
101 | .expect(204) | ||
102 | } | ||
103 | |||
104 | // --------------------------------------------------------------------------- | ||
105 | |||
106 | export { | ||
107 | createUser, | ||
108 | registerUser, | ||
109 | getUserInformation, | ||
110 | getUserVideoRating, | ||
111 | getUsersList, | ||
112 | getUsersListPaginationAndSort, | ||
113 | removeUser, | ||
114 | updateUser | ||
115 | } | ||