api_spec.js 53 KB

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