api_spec.js 53 KB

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