api_spec.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 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/create/', {
  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/create/', {
  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/create/', {
  61. "email": email2,
  62. "password": password2,
  63. }).then(function () {
  64. return chakram.post('/auth/token/create/', {
  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/create/', {
  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/create/', {
  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/destroy/', 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/destroy/', 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/create/', {
  173. "email": email,
  174. "password": password,
  175. }).then(function () {
  176. return chakram.post('/auth/token/create/', {
  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. return chakram.wait();
  219. });
  220. });
  221. });
  222. describe('POST rrsets/ with fresh domain', function () {
  223. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  224. before(function () {
  225. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  226. });
  227. describe("can set an A RRset", function () {
  228. before(function () {
  229. var response = chakram.post(
  230. '/domains/' + domain + '/rrsets/',
  231. {'subname': '', 'type': 'A', 'records': ['127.0.0.1'], 'ttl': 60}
  232. );
  233. expect(response).to.have.status(201);
  234. expect(response).to.have.schema(schemas.rrset);
  235. expect(response).to.have.json('ttl', 60);
  236. expect(response).to.have.json('records', ['127.0.0.1']);
  237. return chakram.wait();
  238. });
  239. itPropagatesToTheApi([
  240. {subname: '', domain: domain, type: 'A', ttl: 60, records: ['127.0.0.1']},
  241. ]);
  242. itShowsUpInPdnsAs('', domain, 'A', ['127.0.0.1'], 60);
  243. });
  244. describe("cannot create RRsets of restricted or dead type", function () {
  245. var rrTypes = ['DNAME', 'ALIAS', 'SOA', 'RRSIG', 'DNSKEY', 'NSEC3PARAM', 'OPT'];
  246. for (var i = 0; i < rrTypes.length; i++) {
  247. var rrType = rrTypes[i];
  248. it(rrType, function () {
  249. return expect(chakram.post(
  250. '/domains/' + domain + '/rrsets/',
  251. {'subname': 'not-welcome', 'type': rrType, 'records': ['127.0.0.1'], 'ttl': 60}
  252. )).to.have.status(400);
  253. });
  254. }
  255. });
  256. it("cannot update RRSets for nonexistent domain name", function () {
  257. return expect(chakram.patch(
  258. '/domains/nonexistent.e2e.domain/rrsets/',
  259. {'subname': '', 'type': 'A', 'records': ['127.0.0.1'], 'ttl': 60}
  260. )).to.have.status(404);
  261. });
  262. it("cannot create RRSets for nonexistent domain name", function () {
  263. return expect(chakram.post(
  264. '/domains/nonexistent.e2e.domain/rrsets/',
  265. {'subname': '', 'type': 'A', 'records': ['127.0.0.1'], 'ttl': 60}
  266. )).to.have.status(404);
  267. });
  268. it("cannot set unicode RRsets", function () {
  269. return expect(chakram.post(
  270. '/domains/' + domain + '/rrsets/',
  271. {'subname': '想不出来', 'type': 'A', 'records': ['127.0.0.1'], 'ttl': 60}
  272. )).to.have.status(422);
  273. });
  274. describe("can set a wildcard AAAA RRset with multiple records", function () {
  275. before(function () {
  276. return chakram.post(
  277. '/domains/' + domain + '/rrsets/',
  278. {'subname': '*.foobar', 'type': 'AAAA', 'records': ['::1', 'bade::affe'], 'ttl': 60}
  279. );
  280. });
  281. itPropagatesToTheApi([
  282. {subname: '*.foobar', domain: domain, type: 'AAAA', ttl: 60, records: ['::1', 'bade::affe']},
  283. {subname: '*.foobar', domain: domain, type: 'AAAA', records: ['bade::affe', '::1']},
  284. ]);
  285. itShowsUpInPdnsAs('test.foobar', domain, 'AAAA', ['::1', 'bade::affe'], 60);
  286. });
  287. describe("can bulk-post an AAAA and an MX record", function () {
  288. before(function () {
  289. var response = chakram.post(
  290. '/domains/' + domain + '/rrsets/',
  291. [
  292. { 'subname': 'ipv6', 'type': 'AAAA', 'records': ['dead::beef'], 'ttl': 22 },
  293. { /* implied: 'subname': '', */ 'type': 'MX', 'records': ['10 mail.example.com.', '20 mail.example.net.'], 'ttl': 33 }
  294. ]
  295. );
  296. expect(response).to.have.status(201);
  297. expect(response).to.have.schema(schemas.rrsets);
  298. return chakram.wait();
  299. });
  300. itPropagatesToTheApi([
  301. {subname: 'ipv6', domain: domain, type: 'AAAA', ttl: 22, records: ['dead::beef']},
  302. {subname: '', domain: domain, type: 'MX', ttl: 33, records: ['10 mail.example.com.', '20 mail.example.net.']},
  303. ]);
  304. itShowsUpInPdnsAs('ipv6', domain, 'AAAA', ['dead::beef'], 22);
  305. itShowsUpInPdnsAs('', domain, 'MX', ['10 mail.example.com.', '20 mail.example.net.'], 33);
  306. });
  307. describe("cannot bulk-post with missing or invalid fields", function () {
  308. before(function () {
  309. // Set an RRset that we'll try to overwrite
  310. var response = chakram.post(
  311. '/domains/' + domain + '/rrsets/',
  312. [{'ttl': 50, 'type': 'TXT', 'records': ['"foo"']}]
  313. );
  314. expect(response).to.have.status(201);
  315. var response = chakram.post(
  316. '/domains/' + domain + '/rrsets/',
  317. [
  318. {'subname': 'a.1', 'records': ['dead::beef'], 'ttl': 22},
  319. {'subname': 'b.1', 'ttl': -50, 'type': 'AAAA', 'records': ['dead::beef']},
  320. {'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  321. {'subname': 'c.1', 'records': ['dead::beef'], 'type': 'AAAA'},
  322. {'subname': 'd.1', 'ttl': 50, 'type': 'AAAA'},
  323. {'subname': 'd.1', 'ttl': 50, 'type': 'SOA', 'records': ['ns1.desec.io. peter.desec.io. 2018034419 10800 3600 604800 60']},
  324. {'subname': 'd.1', 'ttl': 50, 'type': 'OPT', 'records': ['9999']},
  325. {'subname': 'd.1', 'ttl': 50, 'type': 'TYPE099', 'records': ['v=spf1 mx -all']},
  326. ]
  327. );
  328. expect(response).to.have.status(400);
  329. expect(response).to.have.json([
  330. { type: [ 'This field is required.' ] },
  331. { ttl: [ 'Ensure this value is greater than or equal to 1.' ] },
  332. {},
  333. { ttl: [ 'This field is required.' ] },
  334. { records: [ 'This field is required.' ] },
  335. { type: [ 'You cannot tinker with the SOA RRset.' ] },
  336. { type: [ 'You cannot tinker with the OPT RRset.' ] },
  337. { type: [ 'Generic type format is not supported.' ] },
  338. ]);
  339. return chakram.wait();
  340. });
  341. it("does not propagate partially to the API", function () {
  342. return chakram.waitFor([
  343. chakram
  344. .get('/domains/' + domain + '/rrsets/b.1.../AAAA/')
  345. .then(function (response) {
  346. expect(response).to.have.status(404);
  347. }),
  348. chakram
  349. .get('/domains/' + domain + '/rrsets/.../TXT/')
  350. .then(function (response) {
  351. expect(response).to.have.status(200);
  352. expect(response).to.have.json('ttl', 50);
  353. expect(response.body.records).to.have.members(['"foo"']);
  354. }),
  355. ]);
  356. });
  357. itShowsUpInPdnsAs('b.1', domain, 'AAAA', []);
  358. });
  359. context("with a pre-existing RRset", function () {
  360. before(function () {
  361. var response = chakram.post(
  362. '/domains/' + domain + '/rrsets/',
  363. [
  364. {'subname': 'a.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  365. {'subname': 'c.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  366. {'subname': 'delete-test', 'ttl': 50, 'type': 'A', 'records': ['127.1.2.3']},
  367. ]
  368. );
  369. return expect(response).to.have.status(201);
  370. });
  371. describe("can delete an RRset", function () {
  372. before(function () {
  373. var response = chakram.delete('/domains/' + domain + '/rrsets/delete-test.../A/');
  374. return expect(response).to.have.status(204);
  375. });
  376. itPropagatesToTheApi([
  377. {subname: 'delete-test', domain: domain, type: 'A', records: []},
  378. ]);
  379. itShowsUpInPdnsAs('delete-test', domain, 'A', []);
  380. });
  381. describe("cannot bulk-post existing or duplicate RRsets", function () {
  382. var response;
  383. before(function () {
  384. response = chakram.post(
  385. '/domains/' + domain + '/rrsets/',
  386. [
  387. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  388. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  389. ]
  390. );
  391. expect(response).to.have.status(400);
  392. return chakram.wait();
  393. });
  394. it("gives the right response", function () {
  395. expect(response).to.have.json([
  396. { '__all__': [ 'R rset with this Domain, Subname and Type already exists.' ] },
  397. { '__all__': [ 'RRset repeated with same subname and type.' ] },
  398. ]);
  399. return chakram.wait();
  400. });
  401. it("does not touch records in the API", function () {
  402. return chakram
  403. .get('/domains/' + domain + '/rrsets/a.2.../TXT/')
  404. .then(function (response) {
  405. expect(response).to.have.status(200);
  406. expect(response).to.have.json('ttl', 50);
  407. expect(response.body.records).to.have.members(['"foo"']);
  408. });
  409. });
  410. itShowsUpInPdnsAs('a.2', domain, 'TXT', ['"foo"'], 50);
  411. });
  412. describe("cannot delete RRsets via bulk-post", function () {
  413. var response;
  414. before(function () {
  415. response = chakram.post(
  416. '/domains/' + domain + '/rrsets/',
  417. [
  418. {'subname': 'c.2', 'ttl': 40, 'type': 'TXT', 'records': []},
  419. ]
  420. );
  421. return expect(response).to.have.status(400);
  422. });
  423. it("gives the right response", function () {
  424. return expect(response).to.have.json([
  425. { '__all__': [ 'R rset with this Domain, Subname and Type already exists.' ] },
  426. ]);
  427. });
  428. });
  429. });
  430. describe("cannot bulk-post with invalid input", function () {
  431. it("gives the right response for invalid type", function () {
  432. var response = chakram.post(
  433. '/domains/' + domain + '/rrsets/',
  434. [{'subname': 'a.2', 'ttl': 50, 'type': 'INVALID', 'records': ['"foo"']}]
  435. );
  436. return expect(response).to.have.status(422);
  437. });
  438. it("gives the right response for invalid records", function () {
  439. var response = chakram.post(
  440. '/domains/' + domain + '/rrsets/',
  441. [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4']}]
  442. );
  443. return expect(response).to.have.status(422);
  444. });
  445. });
  446. });
  447. describe('PUT rrsets/ with fresh domain', function () {
  448. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  449. before(function () {
  450. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  451. });
  452. describe("can overwrite a single existing RRset using PUT", function () {
  453. before(function () {
  454. var response = chakram.post(
  455. '/domains/' + domain + '/rrsets/',
  456. { 'subname': 'single', 'type': 'AAAA', 'records': ['bade::fefe'], 'ttl': 62 }
  457. ).then(function () {
  458. return chakram.put(
  459. '/domains/' + domain + '/rrsets/single.../AAAA/',
  460. { 'records': ['fefe::bade'], 'ttl': 31 }
  461. );
  462. });
  463. expect(response).to.have.status(200);
  464. expect(response).to.have.schema(schemas.rrset);
  465. return chakram.wait();
  466. });
  467. itPropagatesToTheApi([
  468. {subname: 'single', domain: domain, type: 'AAAA', ttl: 31, records: ['fefe::bade']},
  469. ]);
  470. itShowsUpInPdnsAs('single', domain, 'AAAA', ['fefe::bade'], 31);
  471. });
  472. describe("can bulk-put an AAAA and an MX record", function () {
  473. before(function () {
  474. var response = chakram.put(
  475. '/domains/' + domain + '/rrsets/',
  476. [
  477. { 'subname': 'ipv6', 'type': 'AAAA', 'records': ['dead::beef'], 'ttl': 22 },
  478. { /* implied: 'subname': '', */ 'type': 'MX', 'records': ['10 mail.example.com.', '20 mail.example.net.'], 'ttl': 33 }
  479. ]
  480. );
  481. expect(response).to.have.status(200);
  482. expect(response).to.have.schema(schemas.rrsets);
  483. return chakram.wait();
  484. });
  485. itPropagatesToTheApi([
  486. {subname: 'ipv6', domain: domain, type: 'AAAA', ttl: 22, records: ['dead::beef']},
  487. {subname: '', domain: domain, type: 'MX', ttl: 33, records: ['10 mail.example.com.', '20 mail.example.net.']},
  488. ]);
  489. itShowsUpInPdnsAs('ipv6', domain, 'AAAA', ['dead::beef'], 22);
  490. itShowsUpInPdnsAs('', domain, 'MX', ['10 mail.example.com.', '20 mail.example.net.'], 33);
  491. });
  492. describe("cannot bulk-put with missing or invalid fields", function () {
  493. before(function () {
  494. // Set an RRset that we'll try to overwrite
  495. var response = chakram.put(
  496. '/domains/' + domain + '/rrsets/',
  497. [{'ttl': 50, 'type': 'TXT', 'records': ['"foo"']}]
  498. );
  499. expect(response).to.have.status(200);
  500. var response = chakram.put(
  501. '/domains/' + domain + '/rrsets/',
  502. [
  503. {'subname': 'a.1', 'records': ['dead::beef'], 'ttl': 22},
  504. {'subname': 'b.1', 'ttl': -50, 'type': 'AAAA', 'records': ['dead::beef']},
  505. {'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  506. {'subname': 'c.1', 'records': ['dead::beef'], 'type': 'AAAA'},
  507. {'subname': 'd.1', 'ttl': 50, 'type': 'AAAA'},
  508. ]
  509. );
  510. expect(response).to.have.status(400);
  511. expect(response).to.have.json([
  512. { type: [ 'This field is required.' ] },
  513. { ttl: [ 'Ensure this value is greater than or equal to 1.' ] },
  514. {},
  515. { ttl: [ 'This field is required.' ] },
  516. { records: [ 'This field is required.' ] },
  517. ]);
  518. return chakram.wait();
  519. });
  520. it("does not propagate partially to the API", function () {
  521. return chakram.waitFor([
  522. chakram
  523. .get('/domains/' + domain + '/rrsets/b.1.../AAAA/')
  524. .then(function (response) {
  525. expect(response).to.have.status(404);
  526. }),
  527. chakram
  528. .get('/domains/' + domain + '/rrsets/.../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. });
  536. itShowsUpInPdnsAs('b.1', domain, 'AAAA', []);
  537. });
  538. context("with a pre-existing RRset", function () {
  539. before(function () {
  540. var response = chakram.post(
  541. '/domains/' + domain + '/rrsets/',
  542. [
  543. {'subname': 'a.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  544. {'subname': 'b.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  545. {'subname': 'c.2', 'ttl': 50, 'type': 'A', 'records': ['1.2.3.4']},
  546. ]
  547. );
  548. expect(response).to.have.status(201);
  549. return chakram.wait();
  550. });
  551. describe("can bulk-put existing RRsets", function () {
  552. var response;
  553. before(function () {
  554. response = chakram.put(
  555. '/domains/' + domain + '/rrsets/',
  556. [
  557. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  558. ]
  559. );
  560. expect(response).to.have.status(200);
  561. expect(response).to.have.schema(schemas.rrsets);
  562. return chakram.wait();
  563. });
  564. it("does modify records in the API", function () {
  565. return chakram
  566. .get('/domains/' + domain + '/rrsets/a.2.../TXT/')
  567. .then(function (response) {
  568. expect(response).to.have.status(200);
  569. expect(response).to.have.json('ttl', 40);
  570. expect(response.body.records).to.have.members(['"bar"']);
  571. });
  572. });
  573. itShowsUpInPdnsAs('a.2', domain, 'TXT', ['"bar"'], 40);
  574. });
  575. describe("cannot bulk-put duplicate RRsets", function () {
  576. var response;
  577. before(function () {
  578. response = chakram.put(
  579. '/domains/' + domain + '/rrsets/',
  580. [
  581. {'subname': 'b.2', 'ttl': 60, 'type': 'TXT', 'records': ['"bar"']},
  582. {'subname': 'b.2', 'ttl': 60, 'type': 'TXT', 'records': ['"bar"']},
  583. ]
  584. );
  585. return expect(response).to.have.status(400);
  586. });
  587. it("gives the right response", function () {
  588. return expect(response).to.have.json([
  589. { },
  590. { '__all__': [ 'RRset repeated with same subname and type.' ] },
  591. ]);
  592. });
  593. it("does not touch records in the API", function () {
  594. return chakram
  595. .get('/domains/' + domain + '/rrsets/b.2.../TXT/')
  596. .then(function (response) {
  597. expect(response).to.have.status(200);
  598. expect(response).to.have.json('ttl', 50);
  599. expect(response.body.records).to.have.members(['"foo"']);
  600. });
  601. });
  602. itShowsUpInPdnsAs('b.2', domain, 'TXT', ['"foo"'], 50);
  603. });
  604. describe("can delete RRsets via bulk-put", function () {
  605. var response;
  606. before(function () {
  607. response = chakram.put(
  608. '/domains/' + domain + '/rrsets/',
  609. [
  610. {'subname': 'c.2', 'ttl': 40, 'type': 'A', 'records': []},
  611. ]
  612. );
  613. return expect(response).to.have.status(200);
  614. });
  615. it("gives the right response", function () {
  616. var response = chakram.get('/domains/' + domain + '/rrsets/c.2.../A/');
  617. return expect(response).to.have.status(404);
  618. });
  619. });
  620. });
  621. describe("cannot bulk-put with invalid input", function () {
  622. it("gives the right response for invalid type", function () {
  623. var response = chakram.put(
  624. '/domains/' + domain + '/rrsets/',
  625. [{'subname': 'a.2', 'ttl': 50, 'type': 'INVALID', 'records': ['"foo"']}]
  626. );
  627. return expect(response).to.have.status(422);
  628. });
  629. it("gives the right response for invalid records", function () {
  630. var response = chakram.put(
  631. '/domains/' + domain + '/rrsets/',
  632. [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4']}]
  633. );
  634. return expect(response).to.have.status(422);
  635. });
  636. });
  637. });
  638. describe('PATCH rrsets/ with fresh domain', function () {
  639. var domain = 'e2etest-' + require("uuid").v4() + '.dedyn.io';
  640. before(function () {
  641. return expect(chakram.post('/domains/', {'name': domain})).to.have.status(201);
  642. });
  643. describe("can modify a single existing RRset using PATCH", function () {
  644. before(function () {
  645. var response = chakram.post(
  646. '/domains/' + domain + '/rrsets/',
  647. { 'subname': 'single', 'type': 'AAAA', 'records': ['bade::fefe'], 'ttl': 62 }
  648. ).then(function () {
  649. return chakram.patch(
  650. '/domains/' + domain + '/rrsets/single.../AAAA/',
  651. { 'records': ['fefe::bade'], 'ttl': 31 }
  652. );
  653. });
  654. expect(response).to.have.status(200);
  655. expect(response).to.have.schema(schemas.rrset);
  656. return chakram.wait();
  657. });
  658. itPropagatesToTheApi([
  659. {subname: 'single', domain: domain, type: 'AAAA', ttl: 31, records: ['fefe::bade']},
  660. ]);
  661. itShowsUpInPdnsAs('single', domain, 'AAAA', ['fefe::bade'], 31);
  662. });
  663. describe("can bulk-patch an AAAA and an MX record", function () {
  664. before(function () {
  665. var response = chakram.patch(
  666. '/domains/' + domain + '/rrsets/',
  667. [
  668. { 'subname': 'ipv6', 'type': 'AAAA', 'records': ['dead::beef'], 'ttl': 22 },
  669. { /* implied: 'subname': '', */ 'type': 'MX', 'records': ['10 mail.example.com.', '20 mail.example.net.'], 'ttl': 33 }
  670. ]
  671. );
  672. expect(response).to.have.status(200);
  673. expect(response).to.have.schema(schemas.rrsets);
  674. return chakram.wait();
  675. });
  676. itPropagatesToTheApi([
  677. {subname: 'ipv6', domain: domain, type: 'AAAA', ttl: 22, records: ['dead::beef']},
  678. {subname: '', domain: domain, type: 'MX', ttl: 33, records: ['10 mail.example.com.', '20 mail.example.net.']},
  679. ]);
  680. itShowsUpInPdnsAs('ipv6', domain, 'AAAA', ['dead::beef'], 22);
  681. itShowsUpInPdnsAs('', domain, 'MX', ['10 mail.example.com.', '20 mail.example.net.'], 33);
  682. });
  683. describe("cannot bulk-patch with missing or invalid fields", function () {
  684. before(function () {
  685. // Set an RRset that we'll try to overwrite
  686. var response = chakram.post(
  687. '/domains/' + domain + '/rrsets/',
  688. [{'ttl': 50, 'type': 'TXT', 'records': ['"foo"']}]
  689. );
  690. expect(response).to.have.status(201);
  691. var response = chakram.patch(
  692. '/domains/' + domain + '/rrsets/',
  693. [
  694. {'subname': 'a.1', 'records': ['dead::beef'], 'ttl': 22},
  695. {'subname': 'b.1', 'ttl': -50, 'type': 'AAAA', 'records': ['dead::beef']},
  696. {'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  697. {'subname': 'c.1', 'records': ['dead::beef'], 'type': 'AAAA'},
  698. {'subname': 'd.1', 'ttl': 50, 'type': 'AAAA'},
  699. ]
  700. );
  701. expect(response).to.have.status(400);
  702. expect(response).to.have.json([
  703. { type: [ 'This field is required.' ] },
  704. { ttl: [ 'Ensure this value is greater than or equal to 1.' ] },
  705. {},
  706. {},
  707. {},
  708. ]);
  709. return chakram.wait();
  710. });
  711. it("does not propagate partially to the API", function () {
  712. return chakram.waitFor([
  713. chakram
  714. .get('/domains/' + domain + '/rrsets/b.1.../AAAA/')
  715. .then(function (response) {
  716. expect(response).to.have.status(404);
  717. }),
  718. chakram
  719. .get('/domains/' + domain + '/rrsets/.../TXT/')
  720. .then(function (response) {
  721. expect(response).to.have.status(200);
  722. expect(response).to.have.json('ttl', 50);
  723. expect(response.body.records).to.have.members(['"foo"']);
  724. }),
  725. ]);
  726. });
  727. itShowsUpInPdnsAs('b.1', domain, 'AAAA', []);
  728. });
  729. context("with a pre-existing RRset", function () {
  730. before(function () {
  731. var response = chakram.post(
  732. '/domains/' + domain + '/rrsets/',
  733. [
  734. {'subname': 'a.1', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  735. {'subname': 'a.2', 'ttl': 50, 'type': 'A', 'records': ['4.3.2.1']},
  736. {'subname': 'a.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  737. {'subname': 'b.2', 'ttl': 50, 'type': 'A', 'records': ['5.4.3.2']},
  738. {'subname': 'b.2', 'ttl': 50, 'type': 'TXT', 'records': ['"foo"']},
  739. {'subname': 'c.2', 'ttl': 50, 'type': 'A', 'records': ['1.2.3.4']},
  740. ]
  741. );
  742. return expect(response).to.have.status(201);
  743. });
  744. describe("can bulk-patch existing RRsets", function () {
  745. var response;
  746. before(function () {
  747. response = chakram.patch(
  748. '/domains/' + domain + '/rrsets/',
  749. [
  750. {'subname': 'a.1', 'type': 'TXT', 'records': ['"bar"']},
  751. {'subname': 'a.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  752. ]
  753. );
  754. expect(response).to.have.status(200);
  755. expect(response).to.have.schema(schemas.rrsets);
  756. return chakram.wait();
  757. });
  758. it("does modify records in the API", function () {
  759. return chakram.waitFor([
  760. chakram
  761. .get('/domains/' + domain + '/rrsets/a.1.../TXT/')
  762. .then(function (response) {
  763. expect(response).to.have.status(200);
  764. expect(response).to.have.json('ttl', 50);
  765. expect(response.body.records).to.have.members(['"bar"']);
  766. }),
  767. chakram
  768. .get('/domains/' + domain + '/rrsets/a.2.../TXT/')
  769. .then(function (response) {
  770. expect(response).to.have.status(200);
  771. expect(response).to.have.json('ttl', 40);
  772. expect(response.body.records).to.have.members(['"bar"']);
  773. }),
  774. ]);
  775. });
  776. itShowsUpInPdnsAs('a.2', domain, 'TXT', ['"bar"'], 40);
  777. });
  778. describe("cannot bulk-patch duplicate RRsets", function () {
  779. var response;
  780. before(function () {
  781. response = chakram.patch(
  782. '/domains/' + domain + '/rrsets/',
  783. [
  784. {'subname': 'b.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  785. {'subname': 'b.2', 'ttl': 40, 'type': 'TXT', 'records': ['"bar"']},
  786. ]
  787. );
  788. return expect(response).to.have.status(400);
  789. });
  790. it("gives the right response", function () {
  791. return expect(response).to.have.json([
  792. {},
  793. { '__all__': [ 'RRset repeated with same subname and type.' ] },
  794. ]);
  795. });
  796. it("does not touch records in the API", function () {
  797. return chakram
  798. .get('/domains/' + domain + '/rrsets/b.2.../TXT/')
  799. .then(function (response) {
  800. expect(response).to.have.status(200);
  801. expect(response).to.have.json('ttl', 50);
  802. expect(response.body.records).to.have.members(['"foo"']);
  803. });
  804. });
  805. itShowsUpInPdnsAs('b.2', domain, 'TXT', ['"foo"'], 50);
  806. });
  807. describe("can delete RRsets via bulk-patch", function () {
  808. var response;
  809. before(function () {
  810. response = chakram.patch(
  811. '/domains/' + domain + '/rrsets/',
  812. [
  813. {'subname': 'c.2', 'type': 'A', 'records': []},
  814. ]
  815. );
  816. return expect(response).to.have.status(200);
  817. });
  818. it("gives the right response", function () {
  819. var response = chakram.get('/domains/' + domain + '/rrsets/c.2.../A/');
  820. return expect(response).to.have.status(404);
  821. });
  822. });
  823. describe("accepts missing fields for no-op requests via bulk-patch", function () {
  824. var response;
  825. before(function () {
  826. response = chakram.patch(
  827. '/domains/' + domain + '/rrsets/',
  828. [
  829. {'subname': 'a.2', 'type': 'A', 'records': ['6.6.6.6']}, // existing RRset; TTL not needed
  830. {'subname': 'b.2', 'type': 'A', 'ttl': 40}, // existing RRset; records not needed
  831. {'subname': 'x.2', 'type': 'A', 'records': []}, // non-existent, no-op
  832. {'subname': 'x.2', 'type': 'AAAA'}, // non-existent, no-op
  833. {'subname': 'x.2', 'type': 'TXT', 'ttl': 32}, // non-existent, no-op
  834. ]
  835. );
  836. return expect(response).to.have.status(200);
  837. });
  838. it("gives the right response", function () {
  839. var response = chakram.get('/domains/' + domain + '/rrsets/b.2.../A/');
  840. expect(response).to.have.status(200);
  841. expect(response).to.have.json('ttl', 40);
  842. return chakram.wait();
  843. });
  844. });
  845. describe("catches invalid type for no-op request via bulk-patch", function () {
  846. it("gives the right response", function () {
  847. return chakram.patch(
  848. '/domains/' + domain + '/rrsets/',
  849. [
  850. {'subname': 'x.2', 'type': 'AAA'}, // non-existent, no-op, but invalid type
  851. ]
  852. ).then(function (respObj) {
  853. expect(respObj).to.have.status(422);
  854. expect(respObj.body.detail).to.match(/IN AAA: unknown type given$/);
  855. return chakram.wait();
  856. });
  857. });
  858. });
  859. });
  860. describe("cannot bulk-patch with invalid input", function () {
  861. it("gives the right response for invalid type", function () {
  862. var response = chakram.patch(
  863. '/domains/' + domain + '/rrsets/',
  864. [{'subname': 'a.2', 'ttl': 50, 'type': 'INVALID', 'records': ['"foo"']}]
  865. );
  866. return expect(response).to.have.status(422);
  867. });
  868. it("gives the right response for invalid records", function () {
  869. var response = chakram.patch(
  870. '/domains/' + domain + '/rrsets/',
  871. [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4']}]
  872. );
  873. return expect(response).to.have.status(422);
  874. });
  875. });
  876. });
  877. describe("tokens/ endpoint", function () {
  878. var tokenId;
  879. var tokenValue;
  880. function createTokenWithName () {
  881. var tokenname = "e2e-token-" + require("uuid").v4();
  882. return chakram.post('/tokens/', { name: tokenname }).then(function (response) {
  883. expect(response).to.have.status(201);
  884. expect(response).to.have.json('name', tokenname);
  885. tokenId = response.body['id'];
  886. });
  887. }
  888. function createToken () {
  889. return chakram.post('/tokens/').then(function (response) {
  890. expect(response).to.have.status(201);
  891. tokenId = response.body['id'];
  892. tokenValue = response.body['value'];
  893. });
  894. }
  895. it("can create tokens", createToken);
  896. it("can create tokens with name", createTokenWithName)
  897. describe("with tokens", function () {
  898. before(createToken)
  899. it("a list of tokens can be retrieved", function () {
  900. var response = chakram.get('/tokens/');
  901. return expect(response).to.have.schema(schemas.tokens);
  902. });
  903. describe("can delete token", function () {
  904. before( function () {
  905. var response = chakram.delete('/tokens/' + tokenId + '/');
  906. return expect(response).to.have.status(204);
  907. });
  908. it("deactivates the token", function () {
  909. return expect(chakram.get('/tokens/', {
  910. headers: {'Authorization': 'Token ' + tokenValue }
  911. })).to.have.status(401);
  912. });
  913. });
  914. it("deleting nonexistent tokens yields 204", function () {
  915. var response = chakram.delete('/tokens/wedonthavethisid/');
  916. return expect(response).to.have.status(204);
  917. });
  918. });
  919. })
  920. });
  921. });
  922. });