UNPKG

6.42 kBJavaScriptView Raw
1/*
2 Simulation of responses from a server when we
3 request the creation of hooks.
4
5 nock will simulate a gitlab server running at
6 localhost:80, where Strider Tester, a user is
7 registered with the name "stridertester", and
8 has been registered with api token - zRtVsmeznn7ySatTrnrp
9 stridertester is an "owner" of a group named "testunion"
10 and has admin access to three projects -
11 testunion / unionproject1
12 Strider Tester / pubproject1
13 Strider Tester / privproject1
14 */
15var nock = require('nock');
16
17module.exports = function () {
18
19 //--------------------------------------------------------------------------------------
20 //Simulate a 401 reply if api key is not sent
21 nock('http://localhost:80')
22 .post('/api/v3/projects/5/hooks', {
23 "url": "http://localhost:3000/stridertester/privproject1/api/gitlab/webhook",
24 "push_events": true
25 })
26 .reply(401, {"message": "401 Unauthorized"}, {
27 server: 'nginx',
28 date: 'Fri, 21 Aug 2015 14:42:46 GMT',
29 'content-type': 'application/json',
30 'content-length': '30',
31 connection: 'close',
32 status: '401 Unauthorized',
33 'cache-control': 'no-cache',
34 'x-request-id': '052eafeb-0028-4e69-b951-95925efdc232',
35 'x-runtime': '0.004600'
36 });
37
38 //--------------------------------------------------------------------------------------
39 //Simulate 201 replies when we request the addition of a new project
40 //(adding hooks and keys)
41 nock('http://localhost:80')
42 .post('/api/v3/projects/5/hooks', {
43 "url": "http://localhost:3000/stridertester/privproject1/api/gitlab/webhook",
44 "push_events": true
45 })
46 .query({"private_token": "zRtVsmeznn7ySatTrnrp"})
47 .reply(201, {
48 "id": 18,
49 "url": "http://localhost:3000/stridertester/privproject1/api/gitlab/webhook",
50 "created_at": "2015-08-21T14:11:54.922Z",
51 "project_id": 5,
52 "push_events": true,
53 "issues_events": false,
54 "merge_requests_events": false,
55 "tag_push_events": false
56 }, {
57 server: 'nginx',
58 date: 'Fri, 21 Aug 2015 14:11:54 GMT',
59 'content-type': 'application/json',
60 'content-length': '235',
61 connection: 'close',
62 status: '201 Created',
63 etag: '"3b0821c3d2fa5d74684be993bfba7805"',
64 'cache-control': 'max-age=0, private, must-revalidate',
65 'x-request-id': '0f470a71-564f-4dcc-ae1f-743b73c98473',
66 'x-runtime': '0.021008'
67 });
68
69 nock('http://localhost:80')
70 .post('/api/v3/projects/5/keys', {
71 "title": "strider-stridertester/privproject1",
72 "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5CS04M9YZAoOkl9suZuKdBR643lB9Kv2AigyqcCfljhnFK3tWPLEnlsLE7ZnunTC3VcPPesdC8MeWC95EvQgNGH6Y5oXrGcaxZKiVDunk4xxKpJQjzMbp753B4R7i28NyZVShCyYG0+wkqAYlJ4NFWn6PMEOouinY8Z3/JD/e9luq4QgyyrI9s+e7VWKBBof9f6FtaGWXwpaWt7Peud3/AWKkhPELQmDDBEcZ5jxFneLsO0KEQ3qqGlEhQbtYLblgjWvuekd0CAReyUNjyJQG+rfDyVVadAQNHiri7c8cj6Y766FdfRskCKiA7E5IPfnysUcRx8657u6DG4PC6guV stridertester/privproject1-stridertester@gmail.com\n"
73 })
74 .query({"private_token": "zRtVsmeznn7ySatTrnrp"})
75 .reply(201, {
76 "id": 22,
77 "title": "strider-stridertester/privproject1",
78 "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5CS04M9YZAoOkl9suZuKdBR643lB9Kv2AigyqcCfljhnFK3tWPLEnlsLE7ZnunTC3VcPPesdC8MeWC95EvQgNGH6Y5oXrGcaxZKiVDunk4xxKpJQjzMbp753B4R7i28NyZVShCyYG0+wkqAYlJ4NFWn6PMEOouinY8Z3/JD/e9luq4QgyyrI9s+e7VWKBBof9f6FtaGWXwpaWt7Peud3/AWKkhPELQmDDBEcZ5jxFneLsO0KEQ3qqGlEhQbtYLblgjWvuekd0CAReyUNjyJQG+rfDyVVadAQNHiri7c8cj6Y766FdfRskCKiA7E5IPfnysUcRx8657u6DG4PC6guV stridertester/privproject1-stridertester@gmail.com",
79 "created_at": "2015-08-21T14:11:54.986Z"
80 }, {
81 server: 'nginx',
82 date: 'Fri, 21 Aug 2015 14:11:55 GMT',
83 'content-type': 'application/json',
84 'content-length': '534',
85 connection: 'close',
86 status: '201 Created',
87 etag: '"13f3437db289c9c485011a1201db884f"',
88 'cache-control': 'max-age=0, private, must-revalidate',
89 'x-request-id': '0e829475-9fda-458e-8b34-08c7e8c4c0f5',
90 'x-runtime': '0.099567'
91 });
92
93 //--------------------------------------------------------------------------------------
94 //Simulate a 404 if we passed an incorrect repo id
95 nock('http://localhost:80')
96 .post('/api/v3/projects/invalid-repo/hooks', {
97 "url": "http://localhost:3000/stridertester/privproject1/api/gitlab/webhook",
98 "push_events": true
99 })
100 .query({"private_token": "zRtVsmeznn7ySatTrnrp"})
101 .reply(404, ["1f8b0800000000000003ab56ca4d2d2e4e4c4f55b252323130510828cacf4a4d2e51f0cb2f5170cb2fcd4b51aa050037095a2823000000"], {
102 server: 'nginx',
103 date: 'Fri, 21 Aug 2015 14:47:42 GMT',
104 'content-type': 'application/json',
105 'transfer-encoding': 'chunked',
106 connection: 'close',
107 status: '404 Not Found',
108 'cache-control': 'no-cache',
109 'x-request-id': 'b35510b5-8f3b-434c-bc63-e77ed6a5dc74',
110 'x-runtime': '0.006681',
111 'content-encoding': 'gzip'
112 });
113
114 //--------------------------------------------------------------------------------------
115 //Simulate a 400 if we passed a bad URL
116 nock('http://localhost:80')
117 .post('/api/v3/projects/5/hooks', {"url": false, "push_events": true})
118 .query({"private_token": "zRtVsmeznn7ySatTrnrp"})
119 .reply(400, {"message": "400 (Bad request) \"url\" not given"}, {
120 server: 'nginx',
121 date: 'Fri, 21 Aug 2015 14:52:41 GMT',
122 'content-type': 'application/json',
123 'content-length': '49',
124 connection: 'close',
125 status: '400 Bad Request',
126 'cache-control': 'no-cache',
127 'x-request-id': '823fd96a-3829-4dbe-b7ff-75ab89b50455',
128 'x-runtime': '0.010701'
129 });
130
131 //--------------------------------------------------------------------------------------
132 //Simulate a 401 if wrong credentials were sent
133 nock('http://localhost:80')
134 .post('/api/v3/projects/5/hooks', {
135 "url": "http://localhost:3000/stridertester/privproject1/api/gitlab/webhook",
136 "push_events": true
137 })
138 .query({"private_token": "zRtVsmeznn7ySatTrnra"})
139 .reply(401, {"message": "401 Unauthorized"}, {
140 server: 'nginx',
141 date: 'Fri, 21 Aug 2015 16:38:40 GMT',
142 'content-type': 'application/json',
143 'content-length': '30',
144 connection: 'close',
145 status: '401 Unauthorized',
146 'cache-control': 'no-cache',
147 'x-request-id': '0f3180af-8f43-495f-91fb-8455313eff9a',
148 'x-runtime': '0.004978'
149 });
150};