credentials-crud.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. const test = require('../testlib');
  2. test.run(async function () {
  3. await test('admin', async function (assert, req) {
  4. //Test missing field
  5. var res = await req({
  6. url: '/records/1/credentials',
  7. method: 'post',
  8. data: {
  9. description: 'Test'
  10. }
  11. });
  12. assert.equal(res.status, 422);
  13. //Test invalid type
  14. var res = await req({
  15. url: '/records/1/credentials',
  16. method: 'post',
  17. data: {
  18. description: 'Test',
  19. type: 'foo'
  20. }
  21. });
  22. assert.equal(res.status, 400);
  23. //Test missing key
  24. var res = await req({
  25. url: '/records/1/credentials',
  26. method: 'post',
  27. data: {
  28. description: 'Test',
  29. type: 'key'
  30. }
  31. });
  32. assert.equal(res.status, 422);
  33. //Test missing password
  34. var res = await req({
  35. url: '/records/1/credentials',
  36. method: 'post',
  37. data: {
  38. description: 'Test',
  39. type: 'password'
  40. }
  41. });
  42. assert.equal(res.status, 422);
  43. //Test invalid key
  44. var res = await req({
  45. url: '/records/1/credentials',
  46. method: 'post',
  47. data: {
  48. description: 'Test',
  49. type: 'key',
  50. key: 'foo'
  51. }
  52. });
  53. assert.equal(res.status, 400);
  54. //Test invalid record
  55. var res = await req({
  56. url: '/records/100/credentials',
  57. method: 'post',
  58. data: {
  59. description: 'Test',
  60. type: 'password',
  61. password: 'foo'
  62. }
  63. });
  64. assert.equal(res.status, 404, 'Not existent record should trigger error.');
  65. //Add key (key is intensionally very short but valid) and get it
  66. var res = await req({
  67. url: '/records/1/credentials',
  68. method: 'post',
  69. data: {
  70. description: 'Test Key',
  71. type: 'key',
  72. key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMOLSxmtlYxSkEKep11gjq200PTKVUaA\nyalonAKxw3XnAgMBAAE=\n-----END PUBLIC KEY-----'
  73. }
  74. });
  75. assert.equal(res.status, 201, 'Adding key should succeed.');
  76. assert.equal(res.data, {
  77. id: 4,
  78. description: 'Test Key',
  79. type: 'key',
  80. key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMOLSxmtlYxSkEKep11gjq200PTKVUaA\nyalonAKxw3XnAgMBAAE=\n-----END PUBLIC KEY-----'
  81. }, 'Adding credential data fail.');
  82. var res = await req({
  83. url: '/records/1/credentials/4',
  84. method: 'get'
  85. });
  86. assert.equal(res.status, 200, 'Added key should be found.');
  87. assert.equal(res.data, {
  88. id: 4,
  89. description: 'Test Key',
  90. type: 'key',
  91. key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMOLSxmtlYxSkEKep11gjq200PTKVUaA\nyalonAKxw3XnAgMBAAE=\n-----END PUBLIC KEY-----'
  92. }, 'Added key does not match.');
  93. //Add password and get it
  94. var res = await req({
  95. url: '/records/1/credentials',
  96. method: 'post',
  97. data: {
  98. description: 'Test Password',
  99. type: 'password',
  100. password: 'foo'
  101. }
  102. });
  103. assert.equal(res.status, 201, 'Adding password should succeed.');
  104. assert.equal(res.data, {
  105. id: 5,
  106. description: 'Test Password',
  107. type: 'password',
  108. }, 'Adding credential data fail.');
  109. var res = await req({
  110. url: '/records/1/credentials/5',
  111. method: 'get'
  112. });
  113. assert.equal(res.status, 200, 'Added key should be found.');
  114. assert.equal(res.data, {
  115. id: 5,
  116. description: 'Test Password',
  117. type: 'password',
  118. }, 'Added password does not match.');
  119. //Update credential
  120. var res = await req({
  121. url: '/records/1/credentials/4',
  122. method: 'put',
  123. data: {
  124. type: 'key',
  125. key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMTyWha8C93l2NAPMkLPZ2WnbkqWXOnH\no3RenmVJHn1tAgMBAAE=\n-----END PUBLIC KEY-----'
  126. }
  127. });
  128. assert.equal(res.status, 204, 'Updating record should succeed.');
  129. var res = await req({
  130. url: '/records/1/credentials/4',
  131. method: 'get'
  132. });
  133. assert.equal(res.status, 200, 'Updated credential should be found.');
  134. assert.equal(res.data, {
  135. id: 4,
  136. description: 'Test Key',
  137. type: 'key',
  138. key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMTyWha8C93l2NAPMkLPZ2WnbkqWXOnH\no3RenmVJHn1tAgMBAAE=\n-----END PUBLIC KEY-----'
  139. }, 'Updated key does not match.');
  140. // Change type to password
  141. var res = await req({
  142. url: '/records/1/credentials/4',
  143. method: 'put',
  144. data: {
  145. description: 'Foo Bar',
  146. type: 'password',
  147. password: 'foo'
  148. }
  149. });
  150. assert.equal(res.status, 204, 'Updating record should succeed.');
  151. var res = await req({
  152. url: '/records/1/credentials/4',
  153. method: 'get'
  154. });
  155. assert.equal(res.status, 200, 'Updated credential should be found.');
  156. assert.equal(res.data, {
  157. id: 4,
  158. description: 'Foo Bar',
  159. type: 'password'
  160. }, 'Added key does not match.');
  161. //Test update fails
  162. var res = await req({
  163. url: '/records/1/credentials/4',
  164. method: 'put',
  165. data: {
  166. type: 'foo'
  167. }
  168. });
  169. assert.equal(res.status, 400, 'Invalid type should trigger error.');
  170. var res = await req({
  171. url: '/records/1/credentials/4',
  172. method: 'put',
  173. data: {
  174. type: 'key',
  175. key: 'foo'
  176. }
  177. });
  178. assert.equal(res.status, 400, 'Invalid key should trigger error.');
  179. var res = await req({
  180. url: '/records/1/credentials/4',
  181. method: 'put',
  182. data: {
  183. type: 'key'
  184. }
  185. });
  186. assert.equal(res.status, 422, 'Missing key should trigger error.');
  187. var res = await req({
  188. url: '/records/1/credentials/4',
  189. method: 'put',
  190. data: {
  191. type: 'password'
  192. }
  193. });
  194. assert.equal(res.status, 422, 'Missing password should trigger error.');
  195. var res = await req({
  196. url: '/records/1/credentials/100',
  197. method: 'put',
  198. data: {
  199. description: 'foo'
  200. }
  201. });
  202. assert.equal(res.status, 404, 'Invalid credential should trigger error.');
  203. //Delete entry
  204. var res = await req({
  205. url: '/records/1/credentials/4',
  206. method: 'delete'
  207. });
  208. assert.equal(res.status, 204, 'Deletion of entry should succeed.');
  209. //Delete not existing entry
  210. var res = await req({
  211. url: '/records/1/credentials/100',
  212. method: 'delete'
  213. });
  214. assert.equal(res.status, 404, 'Deletion of not existing entry should fail.');
  215. //Delete entry via wrong record
  216. var res = await req({
  217. url: '/records/4/credentials/5',
  218. method: 'delete'
  219. });
  220. assert.equal(res.status, 404, 'Deletion of entry via wrong record should fail.');
  221. });
  222. await test('user', async function (assert, req) {
  223. //Add password with missing permissions
  224. var res = await req({
  225. url: '/records/4/credentials',
  226. method: 'post',
  227. data: {
  228. description: 'Test Password',
  229. type: 'password',
  230. password: 'foo'
  231. }
  232. });
  233. assert.equal(res.status, 403, 'Adding password should fail for missing permissions.');
  234. //Add password with missing permissions
  235. var res = await req({
  236. url: '/records/1/credentials',
  237. method: 'post',
  238. data: {
  239. description: 'Test Password',
  240. type: 'password',
  241. password: 'foo'
  242. }
  243. });
  244. assert.equal(res.status, 201, 'Adding password should succeed for user.');
  245. assert.equal(res.data, {
  246. id: 6,
  247. description: 'Test Password',
  248. type: 'password',
  249. }, 'Adding credential data fail.');
  250. //Delete entry
  251. var res = await req({
  252. url: '/records/1/credentials/6',
  253. method: 'delete'
  254. });
  255. assert.equal(res.status, 204, 'Deletion of entry should succeed for user.');
  256. //Delete entry without permission
  257. var res = await req({
  258. url: '/records/4/credentials/2',
  259. method: 'delete'
  260. });
  261. assert.equal(res.status, 403, 'Deletion of entry without permission should fail.');
  262. });
  263. });