api_spec.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. var chakram = require("./../setup.js").chakram;
  2. var expect = chakram.expect;
  3. var itPropagatesToTheApi = require("./../setup.js").itPropagatesToTheApi;
  4. var itShowsUpInPdnsAs = require("./../setup.js").itShowsUpInPdnsAs;
  5. var schemas = require("./../schemas.js");
  6. describe("API", function () {
  7. this.timeout(3000);
  8. before(function () {
  9. chakram.setRequestDefaults({
  10. headers: {
  11. 'Host': 'desec.' + process.env.DESECSTACK_DOMAIN,
  12. },
  13. followRedirect: false,
  14. baseUrl: 'https://www/api/v1',
  15. })
  16. });
  17. it("provides an index page", function () {
  18. var response = chakram.get('/');
  19. return expect(response).to.have.status(200);
  20. });
  21. describe("user registration", function () {
  22. it("returns a user object", function () {
  23. var email, password, token;
  24. email = require("uuid").v4() + '@e2etest.local';
  25. password = require("uuid").v4();
  26. var response = chakram.post('/auth/users/create/', {
  27. "email": email,
  28. "password": password,
  29. });
  30. return expect(response).to.have.status(201);
  31. });
  32. it("locks new users that look suspicious");
  33. });
  34. describe("user login", function () {
  35. var email, password, token;
  36. before(function () {
  37. // register a user that we can work with
  38. email = require("uuid").v4() + '@e2etest.local';
  39. password = require("uuid").v4();
  40. var response = chakram.post('/auth/users/create/', {
  41. "email": email,
  42. "password": password,
  43. });
  44. return expect(response).to.have.status(201);
  45. });
  46. it("returns a token", function () {
  47. return chakram.post('/auth/token/create/', {
  48. "email": email,
  49. "password": password,
  50. }).then(function (loginResponse) {
  51. expect(loginResponse.body.auth_token).to.match(schemas.TOKEN_REGEX);
  52. token = loginResponse.body.auth_token;
  53. });
  54. });
  55. describe("token management (djoser)", function () {
  56. var token1, token2;
  57. function createTwoTokens() {
  58. return chakram.waitFor([
  59. chakram.post('/auth/token/create/', {
  60. "email": email,
  61. "password": password,
  62. }).then(function (loginResponse) {
  63. expect(loginResponse).to.have.status(201);
  64. expect(loginResponse.body.auth_token).to.match(schemas.TOKEN_REGEX);
  65. token1 = loginResponse.body.auth_token;
  66. expect(token1).to.not.equal(token2);
  67. }),
  68. chakram.post('/auth/token/create/', {
  69. "email": email,
  70. "password": password,
  71. }).then(function (loginResponse) {
  72. expect(loginResponse).to.have.status(201);
  73. expect(loginResponse.body.auth_token).to.match(schemas.TOKEN_REGEX);
  74. token2 = loginResponse.body.auth_token;
  75. expect(token2).to.not.equal(token1);
  76. })
  77. ]);
  78. }
  79. function deleteToken(token) {
  80. var response = chakram.post('/auth/token/destroy/', null, {
  81. headers: {'Authorization': 'Token ' + token}
  82. });
  83. return expect(response).to.have.status(204);
  84. }
  85. it("can create additional tokens", createTwoTokens);
  86. describe("additional tokens", function () {
  87. before(createTwoTokens);
  88. it("can be used for login (1)", function () {
  89. return expect(chakram.get('/domains/', {
  90. headers: {'Authorization': 'Token ' + token1 }
  91. })).to.have.status(200);
  92. });
  93. it("can be used for login (2)", function () {
  94. return expect(chakram.get('/domains/', {
  95. headers: {'Authorization': 'Token ' + token2 }
  96. })).to.have.status(200);
  97. });
  98. describe("and one deleted", function () {
  99. before(function () {
  100. var response = chakram.post('/auth/token/destroy/', undefined,
  101. { headers: {'Authorization': 'Token ' + token1 } }
  102. );
  103. return expect(response).to.have.status(204);
  104. });
  105. it("leaves the other untouched", function () {
  106. return expect(chakram.get('/domains/', {
  107. headers: {'Authorization': 'Token ' + token2 }
  108. })).to.have.status(200);
  109. });
  110. });
  111. });
  112. });
  113. });
  114. var email = require("uuid").v4() + '@e2etest.local';
  115. describe("with user account [" + email + "]", function () {
  116. var apiHomeSchema = {
  117. properties: {
  118. domains: {type: "string"},
  119. logout: {type: "string"},
  120. user: {type: "string"},
  121. },
  122. required: ["domains", "logout", "user"]
  123. };
  124. var password, token;
  125. before(function () {
  126. chakram.setRequestSettings({
  127. headers: {
  128. 'Host': 'desec.' + process.env.DESECSTACK_DOMAIN,
  129. },
  130. followRedirect: false,
  131. baseUrl: 'https://www/api/v1',
  132. });
  133. // register a user that we can login and work with
  134. password = require("uuid").v4();
  135. return chakram.post('/auth/users/create/', {
  136. "email": email,
  137. "password": password,
  138. }).then(function () {
  139. return chakram.post('/auth/token/create/', {
  140. "email": email,
  141. "password": password,
  142. }).then(function (loginResponse) {
  143. expect(loginResponse.body.auth_token).to.match(schemas.TOKEN_REGEX);
  144. token = loginResponse.body.auth_token;
  145. chakram.setRequestHeader('Authorization', 'Token ' + token);
  146. });
  147. });
  148. });
  149. describe("(logged in)", function () {
  150. describe("api 'homepage'", function () {
  151. var response;
  152. before(function () {
  153. response = chakram.get('/');
  154. });
  155. it('has status 200', function () {
  156. return expect(response).to.have.status(200);
  157. });
  158. it('looks according to the schema', function () {
  159. return expect(response).to.have.schema(apiHomeSchema);
  160. });
  161. });
  162. describe("on domains/ endpoint", function () {
  163. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  164. before(function () {
  165. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  166. });
  167. it("can register a domain name", function () {
  168. var response = chakram.get('/domains/' + domain + '/');
  169. expect(response).to.have.status(200);
  170. expect(response).to.have.schema(schemas.domain);
  171. return chakram.wait();
  172. });
  173. describe("on rrsets/ endpoint", function () {
  174. it("can retrieve RRsets", function () {
  175. var response = chakram.get('/domains/' + domain + '/rrsets/');
  176. expect(response).to.have.status(200);
  177. expect(response).to.have.schema(schemas.rrsets);
  178. response = chakram.get('/domains/' + domain + '/rrsets/.../NS/');
  179. expect(response).to.have.status(200);
  180. expect(response).to.have.schema(schemas.rrset);
  181. return chakram.wait();
  182. });
  183. });
  184. });
  185. describe('POST rrsets/ with fresh domain', function () {
  186. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  187. before(function () {
  188. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  189. });
  190. describe("can set an A RRset", function () {
  191. before(function () {
  192. var response = chakram.post(
  193. '/domains/' + domain + '/rrsets/',
  194. {'subname': '', 'type': 'A', 'records': ['127.0.0.1'], 'ttl': 60}
  195. );
  196. expect(response).to.have.status(201);
  197. expect(response).to.have.schema(schemas.rrset);
  198. expect(response).to.have.json('ttl', 60);
  199. expect(response).to.have.json('records', ['127.0.0.1']);
  200. return chakram.wait();
  201. });
  202. itPropagatesToTheApi([
  203. {subname: '', domain: domain, type: 'A', ttl: 60, records: ['127.0.0.1']},
  204. ]);
  205. itShowsUpInPdnsAs('', domain, 'A', ['127.0.0.1'], 60);
  206. });
  207. describe("can set a wildcard AAAA RRset with multiple records", function () {
  208. before(function () {
  209. return chakram.post(
  210. '/domains/' + domain + '/rrsets/',
  211. {'subname': '*.foobar', 'type': 'AAAA', 'records': ['::1', 'bade::affe'], 'ttl': 60}
  212. );
  213. });
  214. itPropagatesToTheApi([
  215. {subname: '*.foobar', domain: domain, type: 'AAAA', ttl: 60, records: ['::1', 'bade::affe']},
  216. {subname: '*.foobar', domain: domain, type: 'AAAA', records: ['bade::affe', '::1']},
  217. ]);
  218. itShowsUpInPdnsAs('test.foobar', domain, 'AAAA', ['::1', 'bade::affe'], 60);
  219. });
  220. describe("can bulk-post an AAAA and an MX record", function () {
  221. before(function () {
  222. var response = chakram.post(
  223. '/domains/' + domain + '/rrsets/',
  224. [
  225. { 'subname': 'ipv6', 'type': 'AAAA', 'records': ['dead::beef'], 'ttl': 22 },
  226. { /* implied: 'subname': '', */ 'type': 'MX', 'records': ['10 mail.example.com.', '20 mail.example.net.'], 'ttl': 33 }
  227. ]
  228. );
  229. expect(response).to.have.status(201);
  230. expect(response).to.have.schema(schemas.rrsets);
  231. return chakram.wait();
  232. });
  233. itPropagatesToTheApi([
  234. {subname: 'ipv6', domain: domain, type: 'AAAA', ttl: 22, records: ['dead::beef']},
  235. {subname: '', domain: domain, type: 'MX', ttl: 33, records: ['10 mail.example.com.', '20 mail.example.net.']},
  236. ]);
  237. itShowsUpInPdnsAs('ipv6', domain, 'AAAA', ['dead::beef'], 22);
  238. itShowsUpInPdnsAs('', domain, 'MX', ['10 mail.example.com.', '20 mail.example.net.'], 33);
  239. });
  240. describe("cannot bulk-post with missing or invalid fields", function () {
  241. before(function () {
  242. // Set an RRset that we'll try to overwrite
  243. var response = chakram.post(
  244. '/domains/' + domain + '/rrsets/',
  245. [{'ttl': 50, 'type': 'TXT', 'records': ['"foo"']}]
  246. );
  247. expect(response).to.have.status(201);
  248. var response = chakram.post(
  249. '/domains/' + domain + '/rrsets/',
  250. [
  251. {'subname': 'a.1', 'records': ['dead::beef'], 'ttl': 22},
  252. {'subname': 'b.1', 'ttl': -50, 'type': 'AAAA', 'records': ['dead::beef']},
  253. {'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  254. {'subname': 'c.1', 'records': ['dead::beef'], 'type': 'AAAA'},
  255. {'subname': 'd.1', 'ttl': 50, 'type': 'AAAA'},
  256. {'subname': 'd.1', 'ttl': 50, 'type': 'SOA', 'records': ['ns1.desec.io. peter.desec.io. 2018034419 10800 3600 604800 60']},
  257. {'subname': 'd.1', 'ttl': 50, 'type': 'OPT', 'records': ['9999']},
  258. {'subname': 'd.1', 'ttl': 50, 'type': 'TYPE099', 'records': ['v=spf1 mx -all']},
  259. ]
  260. );
  261. expect(response).to.have.status(400);
  262. expect(response).to.have.json([
  263. { type: [ 'This field is required.' ] },
  264. { ttl: [ 'Ensure this value is greater than or equal to 1.' ] },
  265. {},
  266. { ttl: [ 'This field is required.' ] },
  267. { records: [ 'This field is required.' ] },
  268. { type: [ 'You cannot tinker with the SOA RRset.' ] },
  269. { type: [ 'You cannot tinker with the OPT RRset.' ] },
  270. { type: [ 'Generic type format is not supported.' ] },
  271. ]);
  272. return chakram.wait();
  273. });
  274. it("does not propagate partially to the API", function () {
  275. return chakram.waitFor([
  276. chakram
  277. .get('/domains/' + domain + '/rrsets/b.1.../AAAA/')
  278. .then(function (response) {
  279. expect(response).to.have.status(404);
  280. }),
  281. chakram
  282. .get('/domains/' + domain + '/rrsets/.../TXT/')
  283. .then(function (response) {
  284. expect(response).to.have.status(200);
  285. expect(response).to.have.json('ttl', 50);
  286. expect(response.body.records).to.have.members(['"foo"']);
  287. }),
  288. ]);
  289. });
  290. itShowsUpInPdnsAs('b.1', domain, 'AAAA', []);
  291. });
  292. context("with a pre-existing RRset", function () {
  293. before(function () {
  294. var response = chakram.post(
  295. '/domains/' + domain + '/rrsets/',
  296. [
  297. {'subname': 'a.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  298. {'subname': 'c.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  299. {'subname': 'delete-test', 'ttl': 50, 'type': 'A', 'records': ['127.1.2.3']},
  300. ]
  301. );
  302. return expect(response).to.have.status(201);
  303. });
  304. describe("can delete an RRset", function () {
  305. before(function () {
  306. var response = chakram.delete('/domains/' + domain + '/rrsets/delete-test.../A/');
  307. return expect(response).to.have.status(204);
  308. });
  309. itPropagatesToTheApi([
  310. {subname: 'delete-test', domain: domain, type: 'A', records: []},
  311. ]);
  312. itShowsUpInPdnsAs('delete-test', domain, 'A', []);
  313. });
  314. describe("cannot bulk-post existing or duplicate RRsets", function () {
  315. var response;
  316. before(function () {
  317. response = chakram.post(
  318. '/domains/' + domain + '/rrsets/',
  319. [
  320. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  321. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  322. ]
  323. );
  324. expect(response).to.have.status(400);
  325. return chakram.wait();
  326. });
  327. it("gives the right response", function () {
  328. expect(response).to.have.json([
  329. { '__all__': [ 'R rset with this Domain, Subname and Type already exists.' ] },
  330. { '__all__': [ 'RRset repeated with same subname and type.' ] },
  331. ]);
  332. return chakram.wait();
  333. });
  334. it("does not touch records in the API", function () {
  335. return chakram
  336. .get('/domains/' + domain + '/rrsets/a.2.../TXT/')
  337. .then(function (response) {
  338. expect(response).to.have.status(200);
  339. expect(response).to.have.json('ttl', 50);
  340. expect(response.body.records).to.have.members(['"foo"']);
  341. });
  342. });
  343. itShowsUpInPdnsAs('a.2', domain, 'TXT', ['"foo"'], 50);
  344. });
  345. describe("cannot delete RRsets via bulk-post", function () {
  346. var response;
  347. before(function () {
  348. response = chakram.post(
  349. '/domains/' + domain + '/rrsets/',
  350. [
  351. {'subname': 'c.2', 'ttl': 40, 'type': 'TXT', 'records': []},
  352. ]
  353. );
  354. return expect(response).to.have.status(400);
  355. });
  356. it("gives the right response", function () {
  357. return expect(response).to.have.json([
  358. { '__all__': [ 'R rset with this Domain, Subname and Type already exists.' ] },
  359. ]);
  360. });
  361. });
  362. });
  363. describe("cannot bulk-post with invalid input", function () {
  364. it("gives the right response for invalid type", function () {
  365. var response = chakram.post(
  366. '/domains/' + domain + '/rrsets/',
  367. [{'subname': 'a.2', 'ttl': 50, 'type': 'INVALID', 'records': ['"foo"']}]
  368. );
  369. return expect(response).to.have.status(422);
  370. });
  371. it("gives the right response for invalid records", function () {
  372. var response = chakram.post(
  373. '/domains/' + domain + '/rrsets/',
  374. [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4']}]
  375. );
  376. return expect(response).to.have.status(422);
  377. });
  378. });
  379. });
  380. describe('PUT rrsets/ with fresh domain', function () {
  381. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  382. before(function () {
  383. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  384. });
  385. describe("can overwrite a single existing RRset using PUT", function () {
  386. before(function () {
  387. var response = chakram.post(
  388. '/domains/' + domain + '/rrsets/',
  389. { 'subname': 'single', 'type': 'AAAA', 'records': ['bade::fefe'], 'ttl': 62 }
  390. ).then(function () {
  391. return chakram.put(
  392. '/domains/' + domain + '/rrsets/single.../AAAA/',
  393. { 'records': ['fefe::bade'], 'ttl': 31 }
  394. );
  395. });
  396. expect(response).to.have.status(200);
  397. expect(response).to.have.schema(schemas.rrset);
  398. return chakram.wait();
  399. });
  400. itPropagatesToTheApi([
  401. {subname: 'single', domain: domain, type: 'AAAA', ttl: 31, records: ['fefe::bade']},
  402. ]);
  403. itShowsUpInPdnsAs('single', domain, 'AAAA', ['fefe::bade'], 31);
  404. });
  405. describe("can bulk-put an AAAA and an MX record", function () {
  406. before(function () {
  407. var response = chakram.put(
  408. '/domains/' + domain + '/rrsets/',
  409. [
  410. { 'subname': 'ipv6', 'type': 'AAAA', 'records': ['dead::beef'], 'ttl': 22 },
  411. { /* implied: 'subname': '', */ 'type': 'MX', 'records': ['10 mail.example.com.', '20 mail.example.net.'], 'ttl': 33 }
  412. ]
  413. );
  414. expect(response).to.have.status(200);
  415. expect(response).to.have.schema(schemas.rrsets);
  416. return chakram.wait();
  417. });
  418. itPropagatesToTheApi([
  419. {subname: 'ipv6', domain: domain, type: 'AAAA', ttl: 22, records: ['dead::beef']},
  420. {subname: '', domain: domain, type: 'MX', ttl: 33, records: ['10 mail.example.com.', '20 mail.example.net.']},
  421. ]);
  422. itShowsUpInPdnsAs('ipv6', domain, 'AAAA', ['dead::beef'], 22);
  423. itShowsUpInPdnsAs('', domain, 'MX', ['10 mail.example.com.', '20 mail.example.net.'], 33);
  424. });
  425. describe("cannot bulk-put with missing or invalid fields", function () {
  426. before(function () {
  427. // Set an RRset that we'll try to overwrite
  428. var response = chakram.put(
  429. '/domains/' + domain + '/rrsets/',
  430. [{'ttl': 50, 'type': 'TXT', 'records': ['"foo"']}]
  431. );
  432. expect(response).to.have.status(200);
  433. var response = chakram.put(
  434. '/domains/' + domain + '/rrsets/',
  435. [
  436. {'subname': 'a.1', 'records': ['dead::beef'], 'ttl': 22},
  437. {'subname': 'b.1', 'ttl': -50, 'type': 'AAAA', 'records': ['dead::beef']},
  438. {'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  439. {'subname': 'c.1', 'records': ['dead::beef'], 'type': 'AAAA'},
  440. {'subname': 'd.1', 'ttl': 50, 'type': 'AAAA'},
  441. ]
  442. );
  443. expect(response).to.have.status(400);
  444. expect(response).to.have.json([
  445. { type: [ 'This field is required.' ] },
  446. { ttl: [ 'Ensure this value is greater than or equal to 1.' ] },
  447. {},
  448. { ttl: [ 'This field is required.' ] },
  449. { records: [ 'This field is required.' ] },
  450. ]);
  451. return chakram.wait();
  452. });
  453. it("does not propagate partially to the API", function () {
  454. return chakram.waitFor([
  455. chakram
  456. .get('/domains/' + domain + '/rrsets/b.1.../AAAA/')
  457. .then(function (response) {
  458. expect(response).to.have.status(404);
  459. }),
  460. chakram
  461. .get('/domains/' + domain + '/rrsets/.../TXT/')
  462. .then(function (response) {
  463. expect(response).to.have.status(200);
  464. expect(response).to.have.json('ttl', 50);
  465. expect(response.body.records).to.have.members(['"foo"']);
  466. }),
  467. ]);
  468. });
  469. itShowsUpInPdnsAs('b.1', domain, 'AAAA', []);
  470. });
  471. context("with a pre-existing RRset", function () {
  472. before(function () {
  473. var response = chakram.post(
  474. '/domains/' + domain + '/rrsets/',
  475. [
  476. {'subname': 'a.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  477. {'subname': 'b.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  478. {'subname': 'c.2', 'ttl': 50, 'type': 'A', 'records': ['1.2.3.4']},
  479. ]
  480. );
  481. expect(response).to.have.status(201);
  482. return chakram.wait();
  483. });
  484. describe("can bulk-put existing RRsets", function () {
  485. var response;
  486. before(function () {
  487. response = chakram.put(
  488. '/domains/' + domain + '/rrsets/',
  489. [
  490. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  491. ]
  492. );
  493. expect(response).to.have.status(200);
  494. expect(response).to.have.schema(schemas.rrsets);
  495. return chakram.wait();
  496. });
  497. it("does modify records in the API", function () {
  498. return chakram
  499. .get('/domains/' + domain + '/rrsets/a.2.../TXT/')
  500. .then(function (response) {
  501. expect(response).to.have.status(200);
  502. expect(response).to.have.json('ttl', 40);
  503. expect(response.body.records).to.have.members(['"bar"']);
  504. });
  505. });
  506. itShowsUpInPdnsAs('a.2', domain, 'TXT', ['"bar"'], 40);
  507. });
  508. describe("cannot bulk-put duplicate RRsets", function () {
  509. var response;
  510. before(function () {
  511. response = chakram.put(
  512. '/domains/' + domain + '/rrsets/',
  513. [
  514. {'subname': 'b.2', 'ttl': 60, 'type': 'TXT', 'records': ['"bar"']},
  515. {'subname': 'b.2', 'ttl': 60, 'type': 'TXT', 'records': ['"bar"']},
  516. ]
  517. );
  518. return expect(response).to.have.status(400);
  519. });
  520. it("gives the right response", function () {
  521. return expect(response).to.have.json([
  522. { },
  523. { '__all__': [ 'RRset repeated with same subname and type.' ] },
  524. ]);
  525. });
  526. it("does not touch records in the API", function () {
  527. return chakram
  528. .get('/domains/' + domain + '/rrsets/b.2.../TXT/')
  529. .then(function (response) {
  530. expect(response).to.have.status(200);
  531. expect(response).to.have.json('ttl', 50);
  532. expect(response.body.records).to.have.members(['"foo"']);
  533. });
  534. });
  535. itShowsUpInPdnsAs('b.2', domain, 'TXT', ['"foo"'], 50);
  536. });
  537. describe("can delete RRsets via bulk-put", function () {
  538. var response;
  539. before(function () {
  540. response = chakram.put(
  541. '/domains/' + domain + '/rrsets/',
  542. [
  543. {'subname': 'c.2', 'ttl': 40, 'type': 'A', 'records': []},
  544. ]
  545. );
  546. return expect(response).to.have.status(200);
  547. });
  548. it("gives the right response", function () {
  549. var response = chakram.get('/domains/' + domain + '/rrsets/c.2.../A/');
  550. return expect(response).to.have.status(404);
  551. });
  552. });
  553. });
  554. describe("cannot bulk-put with invalid input", function () {
  555. it("gives the right response for invalid type", function () {
  556. var response = chakram.put(
  557. '/domains/' + domain + '/rrsets/',
  558. [{'subname': 'a.2', 'ttl': 50, 'type': 'INVALID', 'records': ['"foo"']}]
  559. );
  560. return expect(response).to.have.status(422);
  561. });
  562. it("gives the right response for invalid records", function () {
  563. var response = chakram.put(
  564. '/domains/' + domain + '/rrsets/',
  565. [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4']}]
  566. );
  567. return expect(response).to.have.status(422);
  568. });
  569. });
  570. });
  571. describe('PATCH rrsets/ with fresh domain', function () {
  572. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  573. before(function () {
  574. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  575. });
  576. describe("can modify a single existing RRset using PATCH", function () {
  577. before(function () {
  578. var response = chakram.post(
  579. '/domains/' + domain + '/rrsets/',
  580. { 'subname': 'single', 'type': 'AAAA', 'records': ['bade::fefe'], 'ttl': 62 }
  581. ).then(function () {
  582. return chakram.patch(
  583. '/domains/' + domain + '/rrsets/single.../AAAA/',
  584. { 'records': ['fefe::bade'], 'ttl': 31 }
  585. );
  586. });
  587. expect(response).to.have.status(200);
  588. expect(response).to.have.schema(schemas.rrset);
  589. return chakram.wait();
  590. });
  591. itPropagatesToTheApi([
  592. {subname: 'single', domain: domain, type: 'AAAA', ttl: 31, records: ['fefe::bade']},
  593. ]);
  594. itShowsUpInPdnsAs('single', domain, 'AAAA', ['fefe::bade'], 31);
  595. });
  596. describe("can bulk-patch an AAAA and an MX record", function () {
  597. before(function () {
  598. var response = chakram.patch(
  599. '/domains/' + domain + '/rrsets/',
  600. [
  601. { 'subname': 'ipv6', 'type': 'AAAA', 'records': ['dead::beef'], 'ttl': 22 },
  602. { /* implied: 'subname': '', */ 'type': 'MX', 'records': ['10 mail.example.com.', '20 mail.example.net.'], 'ttl': 33 }
  603. ]
  604. );
  605. expect(response).to.have.status(200);
  606. expect(response).to.have.schema(schemas.rrsets);
  607. return chakram.wait();
  608. });
  609. itPropagatesToTheApi([
  610. {subname: 'ipv6', domain: domain, type: 'AAAA', ttl: 22, records: ['dead::beef']},
  611. {subname: '', domain: domain, type: 'MX', ttl: 33, records: ['10 mail.example.com.', '20 mail.example.net.']},
  612. ]);
  613. itShowsUpInPdnsAs('ipv6', domain, 'AAAA', ['dead::beef'], 22);
  614. itShowsUpInPdnsAs('', domain, 'MX', ['10 mail.example.com.', '20 mail.example.net.'], 33);
  615. });
  616. describe("cannot bulk-patch with missing or invalid fields", function () {
  617. before(function () {
  618. // Set an RRset that we'll try to overwrite
  619. var response = chakram.post(
  620. '/domains/' + domain + '/rrsets/',
  621. [{'ttl': 50, 'type': 'TXT', 'records': ['"foo"']}]
  622. );
  623. expect(response).to.have.status(201);
  624. var response = chakram.patch(
  625. '/domains/' + domain + '/rrsets/',
  626. [
  627. {'subname': 'a.1', 'records': ['dead::beef'], 'ttl': 22},
  628. {'subname': 'b.1', 'ttl': -50, 'type': 'AAAA', 'records': ['dead::beef']},
  629. {'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  630. {'subname': 'c.1', 'records': ['dead::beef'], 'type': 'AAAA'},
  631. {'subname': 'd.1', 'ttl': 50, 'type': 'AAAA'},
  632. ]
  633. );
  634. expect(response).to.have.status(400);
  635. expect(response).to.have.json([
  636. { type: [ 'This field is required.' ] },
  637. { ttl: [ 'Ensure this value is greater than or equal to 1.' ] },
  638. {},
  639. {},
  640. {},
  641. ]);
  642. return chakram.wait();
  643. });
  644. it("does not propagate partially to the API", function () {
  645. return chakram.waitFor([
  646. chakram
  647. .get('/domains/' + domain + '/rrsets/b.1.../AAAA/')
  648. .then(function (response) {
  649. expect(response).to.have.status(404);
  650. }),
  651. chakram
  652. .get('/domains/' + domain + '/rrsets/.../TXT/')
  653. .then(function (response) {
  654. expect(response).to.have.status(200);
  655. expect(response).to.have.json('ttl', 50);
  656. expect(response.body.records).to.have.members(['"foo"']);
  657. }),
  658. ]);
  659. });
  660. itShowsUpInPdnsAs('b.1', domain, 'AAAA', []);
  661. });
  662. context("with a pre-existing RRset", function () {
  663. before(function () {
  664. var response = chakram.post(
  665. '/domains/' + domain + '/rrsets/',
  666. [
  667. {'subname': 'a.1', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  668. {'subname': 'a.2', 'ttl': 50, 'type': 'A', 'records': ['4.3.2.1']},
  669. {'subname': 'a.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  670. {'subname': 'b.2', 'ttl': 50, 'type': 'A', 'records': ['5.4.3.2']},
  671. {'subname': 'b.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  672. {'subname': 'c.2', 'ttl': 50, 'type': 'A', 'records': ['1.2.3.4']},
  673. ]
  674. );
  675. return expect(response).to.have.status(201);
  676. });
  677. describe("can bulk-patch existing RRsets", function () {
  678. var response;
  679. before(function () {
  680. response = chakram.patch(
  681. '/domains/' + domain + '/rrsets/',
  682. [
  683. {'subname': 'a.1', 'type': 'TXT', 'records': ['"bar"']},
  684. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  685. ]
  686. );
  687. expect(response).to.have.status(200);
  688. expect(response).to.have.schema(schemas.rrsets);
  689. return chakram.wait();
  690. });
  691. it("does modify records in the API", function () {
  692. return chakram.waitFor([
  693. chakram
  694. .get('/domains/' + domain + '/rrsets/a.1.../TXT/')
  695. .then(function (response) {
  696. expect(response).to.have.status(200);
  697. expect(response).to.have.json('ttl', 50);
  698. expect(response.body.records).to.have.members(['"bar"']);
  699. }),
  700. chakram
  701. .get('/domains/' + domain + '/rrsets/a.2.../TXT/')
  702. .then(function (response) {
  703. expect(response).to.have.status(200);
  704. expect(response).to.have.json('ttl', 40);
  705. expect(response.body.records).to.have.members(['"bar"']);
  706. }),
  707. ]);
  708. });
  709. itShowsUpInPdnsAs('a.2', domain, 'TXT', ['"bar"'], 40);
  710. });
  711. describe("cannot bulk-patch duplicate RRsets", function () {
  712. var response;
  713. before(function () {
  714. response = chakram.patch(
  715. '/domains/' + domain + '/rrsets/',
  716. [
  717. {'subname': 'b.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  718. {'subname': 'b.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  719. ]
  720. );
  721. return expect(response).to.have.status(400);
  722. });
  723. it("gives the right response", function () {
  724. return expect(response).to.have.json([
  725. {},
  726. { '__all__': [ 'RRset repeated with same subname and type.' ] },
  727. ]);
  728. });
  729. it("does not touch records in the API", function () {
  730. return chakram
  731. .get('/domains/' + domain + '/rrsets/b.2.../TXT/')
  732. .then(function (response) {
  733. expect(response).to.have.status(200);
  734. expect(response).to.have.json('ttl', 50);
  735. expect(response.body.records).to.have.members(['"foo"']);
  736. });
  737. });
  738. itShowsUpInPdnsAs('b.2', domain, 'TXT', ['"foo"'], 50);
  739. });
  740. describe("can delete RRsets via bulk-patch", function () {
  741. var response;
  742. before(function () {
  743. response = chakram.patch(
  744. '/domains/' + domain + '/rrsets/',
  745. [
  746. {'subname': 'c.2', 'type': 'A', 'records': []},
  747. ]
  748. );
  749. return expect(response).to.have.status(200);
  750. });
  751. it("gives the right response", function () {
  752. var response = chakram.get('/domains/' + domain + '/rrsets/c.2.../A/');
  753. return expect(response).to.have.status(404);
  754. });
  755. });
  756. describe("accepts missing fields for no-op requests via bulk-patch", function () {
  757. var response;
  758. before(function () {
  759. response = chakram.patch(
  760. '/domains/' + domain + '/rrsets/',
  761. [
  762. {'subname': 'a.2', 'type': 'A', 'records': ['6.6.6.6']}, // existing RRset; TTL not needed
  763. {'subname': 'b.2', 'type': 'A', 'ttl': 40}, // existing RRset; records not needed
  764. {'subname': 'x.2', 'type': 'A', 'records': []}, // non-existent, no-op
  765. {'subname': 'x.2', 'type': 'AAAA'}, // non-existent, no-op
  766. {'subname': 'x.2', 'type': 'TXT', 'ttl': 32}, // non-existent, no-op
  767. ]
  768. );
  769. return expect(response).to.have.status(200);
  770. });
  771. it("gives the right response", function () {
  772. var response = chakram.get('/domains/' + domain + '/rrsets/b.2.../A/');
  773. expect(response).to.have.status(200);
  774. expect(response).to.have.json('ttl', 40);
  775. return chakram.wait();
  776. });
  777. });
  778. describe("catches invalid type for no-op request via bulk-patch", function () {
  779. it("gives the right response", function () {
  780. return chakram.patch(
  781. '/domains/' + domain + '/rrsets/',
  782. [
  783. {'subname': 'x.2', 'type': 'AAA'}, // non-existent, no-op, but invalid type
  784. ]
  785. ).then(function (respObj) {
  786. expect(respObj).to.have.status(422);
  787. expect(respObj.body.detail).to.match(/IN AAA: unknown type given$/);
  788. return chakram.wait();
  789. });
  790. });
  791. });
  792. });
  793. describe("cannot bulk-patch with invalid input", function () {
  794. it("gives the right response for invalid type", function () {
  795. var response = chakram.patch(
  796. '/domains/' + domain + '/rrsets/',
  797. [{'subname': 'a.2', 'ttl': 50, 'type': 'INVALID', 'records': ['"foo"']}]
  798. );
  799. return expect(response).to.have.status(422);
  800. });
  801. it("gives the right response for invalid records", function () {
  802. var response = chakram.patch(
  803. '/domains/' + domain + '/rrsets/',
  804. [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4']}]
  805. );
  806. return expect(response).to.have.status(422);
  807. });
  808. });
  809. });
  810. describe("tokens/ endpoint", function () {
  811. var tokenId;
  812. var tokenValue;
  813. function createTokenWithName () {
  814. var tokenname = "e2e-token-" + require("uuid").v4();
  815. return chakram.post('/tokens/', { name: tokenname }).then(function (response) {
  816. expect(response).to.have.status(201);
  817. expect(response).to.have.json('name', tokenname);
  818. tokenId = response.body['id'];
  819. });
  820. }
  821. function createToken () {
  822. return chakram.post('/tokens/').then(function (response) {
  823. expect(response).to.have.status(201);
  824. tokenId = response.body['id'];
  825. tokenValue = response.body['value'];
  826. });
  827. }
  828. it("can create tokens", createToken);
  829. it("can create tokens with name", createTokenWithName)
  830. describe("with tokens", function () {
  831. before(createToken)
  832. it("a list of tokens can be retrieved", function () {
  833. var response = chakram.get('/tokens/');
  834. return expect(response).to.have.schema(schemas.tokens);
  835. });
  836. describe("can delete token", function () {
  837. before( function () {
  838. var response = chakram.delete('/tokens/' + tokenId + '/');
  839. return expect(response).to.have.status(204);
  840. });
  841. it("deactivates the token", function () {
  842. return expect(chakram.get('/tokens/', {
  843. headers: {'Authorization': 'Token ' + tokenValue }
  844. })).to.have.status(401);
  845. });
  846. });
  847. it("deleting nonexistent tokens yields 204", function () {
  848. var response = chakram.delete('/tokens/wedonthavethisid/');
  849. return expect(response).to.have.status(204);
  850. });
  851. });
  852. })
  853. });
  854. });
  855. });