api_spec.js 56 KB

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