api_spec.js 54 KB

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