diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-28 13:59:22 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-28 13:59:22 +0100 |
commit | c5d31dba56d669c0df0209761c43c5a6ac7cec4a (patch) | |
tree | fe72b56a1c0e7beb6e092c393a00ddfe93a5d71f /server/tests/utils/users | |
parent | db799da3d2b2ea465165df78ff71effa653b6309 (diff) | |
download | PeerTube-c5d31dba56d669c0df0209761c43c5a6ac7cec4a.tar.gz PeerTube-c5d31dba56d669c0df0209761c43c5a6ac7cec4a.tar.zst PeerTube-c5d31dba56d669c0df0209761c43c5a6ac7cec4a.zip |
Tests directories refractor
Diffstat (limited to 'server/tests/utils/users')
-rw-r--r-- | server/tests/utils/users/login.ts | 59 | ||||
-rw-r--r-- | server/tests/utils/users/users.ts | 161 |
2 files changed, 220 insertions, 0 deletions
diff --git a/server/tests/utils/users/login.ts b/server/tests/utils/users/login.ts new file mode 100644 index 000000000..855c4828d --- /dev/null +++ b/server/tests/utils/users/login.ts | |||
@@ -0,0 +1,59 @@ | |||
1 | import * as request from 'supertest' | ||
2 | |||
3 | import { ServerInfo } from '../server/servers' | ||
4 | |||
5 | type Client = { id: string, secret: string } | ||
6 | type User = { username: string, password: string } | ||
7 | type Server = { url: string, client: Client, user: User } | ||
8 | |||
9 | function login (url: string, client: Client, user: User, expectedStatus = 200) { | ||
10 | const path = '/api/v1/users/token' | ||
11 | |||
12 | const body = { | ||
13 | client_id: client.id, | ||
14 | client_secret: client.secret, | ||
15 | username: user.username, | ||
16 | password: user.password, | ||
17 | response_type: 'code', | ||
18 | grant_type: 'password', | ||
19 | scope: 'upload' | ||
20 | } | ||
21 | |||
22 | return request(url) | ||
23 | .post(path) | ||
24 | .type('form') | ||
25 | .send(body) | ||
26 | .expect(expectedStatus) | ||
27 | } | ||
28 | |||
29 | async function loginAndGetAccessToken (server: Server) { | ||
30 | const res = await login(server.url, server.client, server.user, 200) | ||
31 | |||
32 | return res.body.access_token as string | ||
33 | } | ||
34 | |||
35 | async function getUserAccessToken (server: Server, user: User) { | ||
36 | const res = await login(server.url, server.client, user, 200) | ||
37 | |||
38 | return res.body.access_token as string | ||
39 | } | ||
40 | |||
41 | function setAccessTokensToServers (servers: ServerInfo[]) { | ||
42 | const tasks: Promise<any>[] = [] | ||
43 | |||
44 | for (const server of servers) { | ||
45 | const p = loginAndGetAccessToken(server).then(t => server.accessToken = t) | ||
46 | tasks.push(p) | ||
47 | } | ||
48 | |||
49 | return Promise.all(tasks) | ||
50 | } | ||
51 | |||
52 | // --------------------------------------------------------------------------- | ||
53 | |||
54 | export { | ||
55 | login, | ||
56 | loginAndGetAccessToken, | ||
57 | getUserAccessToken, | ||
58 | setAccessTokensToServers | ||
59 | } | ||
diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts new file mode 100644 index 000000000..bd8d7ab04 --- /dev/null +++ b/server/tests/utils/users/users.ts | |||
@@ -0,0 +1,161 @@ | |||
1 | import * as request from 'supertest' | ||
2 | |||
3 | import { UserRole } from '../../../../shared/index' | ||
4 | |||
5 | function createUser ( | ||
6 | url: string, | ||
7 | accessToken: string, | ||
8 | username: string, | ||
9 | password: string, | ||
10 | videoQuota = 1000000, | ||
11 | role: UserRole = UserRole.USER, | ||
12 | specialStatus = 204 | ||
13 | ) { | ||
14 | const path = '/api/v1/users' | ||
15 | const body = { | ||
16 | username, | ||
17 | password, | ||
18 | role, | ||
19 | email: username + '@example.com', | ||
20 | videoQuota | ||
21 | } | ||
22 | |||
23 | return request(url) | ||
24 | .post(path) | ||
25 | .set('Accept', 'application/json') | ||
26 | .set('Authorization', 'Bearer ' + accessToken) | ||
27 | .send(body) | ||
28 | .expect(specialStatus) | ||
29 | } | ||
30 | |||
31 | function registerUser (url: string, username: string, password: string, specialStatus = 204) { | ||
32 | const path = '/api/v1/users/register' | ||
33 | const body = { | ||
34 | username, | ||
35 | password, | ||
36 | email: username + '@example.com' | ||
37 | } | ||
38 | |||
39 | return request(url) | ||
40 | .post(path) | ||
41 | .set('Accept', 'application/json') | ||
42 | .send(body) | ||
43 | .expect(specialStatus) | ||
44 | } | ||
45 | |||
46 | function getMyUserInformation (url: string, accessToken: string) { | ||
47 | const path = '/api/v1/users/me' | ||
48 | |||
49 | return request(url) | ||
50 | .get(path) | ||
51 | .set('Accept', 'application/json') | ||
52 | .set('Authorization', 'Bearer ' + accessToken) | ||
53 | .expect(200) | ||
54 | .expect('Content-Type', /json/) | ||
55 | } | ||
56 | |||
57 | function getUserInformation (url: string, accessToken: string, userId: number) { | ||
58 | const path = '/api/v1/users/' + userId | ||
59 | |||
60 | return request(url) | ||
61 | .get(path) | ||
62 | .set('Accept', 'application/json') | ||
63 | .set('Authorization', 'Bearer ' + accessToken) | ||
64 | .expect(200) | ||
65 | .expect('Content-Type', /json/) | ||
66 | } | ||
67 | |||
68 | function getUserVideoRating (url: string, accessToken: string, videoId: number) { | ||
69 | const path = '/api/v1/users/me/videos/' + videoId + '/rating' | ||
70 | |||
71 | return request(url) | ||
72 | .get(path) | ||
73 | .set('Accept', 'application/json') | ||
74 | .set('Authorization', 'Bearer ' + accessToken) | ||
75 | .expect(200) | ||
76 | .expect('Content-Type', /json/) | ||
77 | } | ||
78 | |||
79 | function getUsersList (url: string, accessToken: string) { | ||
80 | const path = '/api/v1/users' | ||
81 | |||
82 | return request(url) | ||
83 | .get(path) | ||
84 | .set('Accept', 'application/json') | ||
85 | .set('Authorization', 'Bearer ' + accessToken) | ||
86 | .expect(200) | ||
87 | .expect('Content-Type', /json/) | ||
88 | } | ||
89 | |||
90 | function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string) { | ||
91 | const path = '/api/v1/users' | ||
92 | |||
93 | return request(url) | ||
94 | .get(path) | ||
95 | .query({ start }) | ||
96 | .query({ count }) | ||
97 | .query({ sort }) | ||
98 | .set('Accept', 'application/json') | ||
99 | .set('Authorization', 'Bearer ' + accessToken) | ||
100 | .expect(200) | ||
101 | .expect('Content-Type', /json/) | ||
102 | } | ||
103 | |||
104 | function removeUser (url: string, userId: number, accessToken: string, expectedStatus = 204) { | ||
105 | const path = '/api/v1/users' | ||
106 | |||
107 | return request(url) | ||
108 | .delete(path + '/' + userId) | ||
109 | .set('Accept', 'application/json') | ||
110 | .set('Authorization', 'Bearer ' + accessToken) | ||
111 | .expect(expectedStatus) | ||
112 | } | ||
113 | |||
114 | function updateMyUser (url: string, accessToken: string, newPassword: string, displayNSFW?: boolean, | ||
115 | email?: string, autoPlayVideo?: boolean) { | ||
116 | const path = '/api/v1/users/me' | ||
117 | |||
118 | const toSend = {} | ||
119 | if (newPassword !== undefined && newPassword !== null) toSend['password'] = newPassword | ||
120 | if (displayNSFW !== undefined && displayNSFW !== null) toSend['displayNSFW'] = displayNSFW | ||
121 | if (autoPlayVideo !== undefined && autoPlayVideo !== null) toSend['autoPlayVideo'] = autoPlayVideo | ||
122 | if (email !== undefined && email !== null) toSend['email'] = email | ||
123 | |||
124 | return request(url) | ||
125 | .put(path) | ||
126 | .set('Accept', 'application/json') | ||
127 | .set('Authorization', 'Bearer ' + accessToken) | ||
128 | .send(toSend) | ||
129 | .expect(204) | ||
130 | } | ||
131 | |||
132 | function updateUser (url: string, userId: number, accessToken: string, email: string, videoQuota: number, role: UserRole) { | ||
133 | const path = '/api/v1/users/' + userId | ||
134 | |||
135 | const toSend = {} | ||
136 | if (email !== undefined && email !== null) toSend['email'] = email | ||
137 | if (videoQuota !== undefined && videoQuota !== null) toSend['videoQuota'] = videoQuota | ||
138 | if (role !== undefined && role !== null) toSend['role'] = role | ||
139 | |||
140 | return request(url) | ||
141 | .put(path) | ||
142 | .set('Accept', 'application/json') | ||
143 | .set('Authorization', 'Bearer ' + accessToken) | ||
144 | .send(toSend) | ||
145 | .expect(204) | ||
146 | } | ||
147 | |||
148 | // --------------------------------------------------------------------------- | ||
149 | |||
150 | export { | ||
151 | createUser, | ||
152 | registerUser, | ||
153 | getMyUserInformation, | ||
154 | getUserVideoRating, | ||
155 | getUsersList, | ||
156 | getUsersListPaginationAndSort, | ||
157 | removeUser, | ||
158 | updateUser, | ||
159 | updateMyUser, | ||
160 | getUserInformation | ||
161 | } | ||