api_spec.js 52 KB

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