123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- const test = require('../testlib');
- test.run(async function () {
- await test('admin', async function (assert, req) {
- //Test missing field
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test'
- }
- });
- assert.equal(res.status, 422);
- //Test invalid type
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test',
- type: 'foo'
- }
- });
- assert.equal(res.status, 400);
- //Test missing key
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test',
- type: 'key'
- }
- });
- assert.equal(res.status, 422);
- //Test missing password
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test',
- type: 'password'
- }
- });
- assert.equal(res.status, 422);
- //Test invalid key
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test',
- type: 'key',
- key: 'foo'
- }
- });
- assert.equal(res.status, 400);
- //Test invalid record
- var res = await req({
- url: '/records/100/credentials',
- method: 'post',
- data: {
- description: 'Test',
- type: 'password',
- password: 'foo'
- }
- });
- assert.equal(res.status, 404, 'Not existent record should trigger error.');
- //Add key (key is intensionally very short but valid) and get it
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test Key',
- type: 'key',
- key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMOLSxmtlYxSkEKep11gjq200PTKVUaA\nyalonAKxw3XnAgMBAAE=\n-----END PUBLIC KEY-----'
- }
- });
- assert.equal(res.status, 201, 'Adding key should succeed.');
- assert.equal(res.data, {
- id: 4,
- description: 'Test Key',
- type: 'key',
- key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMOLSxmtlYxSkEKep11gjq200PTKVUaA\nyalonAKxw3XnAgMBAAE=\n-----END PUBLIC KEY-----'
- }, 'Adding credential data fail.');
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'get'
- });
- assert.equal(res.status, 200, 'Added key should be found.');
- assert.equal(res.data, {
- id: 4,
- description: 'Test Key',
- type: 'key',
- key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMOLSxmtlYxSkEKep11gjq200PTKVUaA\nyalonAKxw3XnAgMBAAE=\n-----END PUBLIC KEY-----'
- }, 'Added key does not match.');
- //Add password and get it
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test Password',
- type: 'password',
- password: 'foo'
- }
- });
- assert.equal(res.status, 201, 'Adding password should succeed.');
- assert.equal(res.data, {
- id: 5,
- description: 'Test Password',
- type: 'password',
- }, 'Adding credential data fail.');
- var res = await req({
- url: '/records/1/credentials/5',
- method: 'get'
- });
- assert.equal(res.status, 200, 'Added key should be found.');
- assert.equal(res.data, {
- id: 5,
- description: 'Test Password',
- type: 'password',
- }, 'Added password does not match.');
- //Update credential
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'put',
- data: {
- type: 'key',
- key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMTyWha8C93l2NAPMkLPZ2WnbkqWXOnH\no3RenmVJHn1tAgMBAAE=\n-----END PUBLIC KEY-----'
- }
- });
- assert.equal(res.status, 204, 'Updating record should succeed.');
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'get'
- });
- assert.equal(res.status, 200, 'Updated credential should be found.');
- assert.equal(res.data, {
- id: 4,
- description: 'Test Key',
- type: 'key',
- key: '-----BEGIN PUBLIC KEY-----\nMDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMTyWha8C93l2NAPMkLPZ2WnbkqWXOnH\no3RenmVJHn1tAgMBAAE=\n-----END PUBLIC KEY-----'
- }, 'Updated key does not match.');
- // Change type to password
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'put',
- data: {
- description: 'Foo Bar',
- type: 'password',
- password: 'foo'
- }
- });
- assert.equal(res.status, 204, 'Updating record should succeed.');
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'get'
- });
- assert.equal(res.status, 200, 'Updated credential should be found.');
- assert.equal(res.data, {
- id: 4,
- description: 'Foo Bar',
- type: 'password'
- }, 'Added key does not match.');
- //Test update fails
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'put',
- data: {
- type: 'foo'
- }
- });
- assert.equal(res.status, 400, 'Invalid type should trigger error.');
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'put',
- data: {
- type: 'key',
- key: 'foo'
- }
- });
- assert.equal(res.status, 400, 'Invalid key should trigger error.');
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'put',
- data: {
- type: 'key'
- }
- });
- assert.equal(res.status, 422, 'Missing key should trigger error.');
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'put',
- data: {
- type: 'password'
- }
- });
- assert.equal(res.status, 422, 'Missing password should trigger error.');
- var res = await req({
- url: '/records/1/credentials/100',
- method: 'put',
- data: {
- description: 'foo'
- }
- });
- assert.equal(res.status, 404, 'Invalid credential should trigger error.');
- //Delete entry
- var res = await req({
- url: '/records/1/credentials/4',
- method: 'delete'
- });
- assert.equal(res.status, 204, 'Deletion of entry should succeed.');
- //Delete not existing entry
- var res = await req({
- url: '/records/1/credentials/100',
- method: 'delete'
- });
- assert.equal(res.status, 404, 'Deletion of not existing entry should fail.');
- //Delete entry via wrong record
- var res = await req({
- url: '/records/4/credentials/5',
- method: 'delete'
- });
- assert.equal(res.status, 404, 'Deletion of entry via wrong record should fail.');
- });
- await test('user', async function (assert, req) {
- //Add password with missing permissions
- var res = await req({
- url: '/records/4/credentials',
- method: 'post',
- data: {
- description: 'Test Password',
- type: 'password',
- password: 'foo'
- }
- });
- assert.equal(res.status, 403, 'Adding password should fail for missing permissions.');
- //Add password with missing permissions
- var res = await req({
- url: '/records/1/credentials',
- method: 'post',
- data: {
- description: 'Test Password',
- type: 'password',
- password: 'foo'
- }
- });
- assert.equal(res.status, 201, 'Adding password should succeed for user.');
- assert.equal(res.data, {
- id: 6,
- description: 'Test Password',
- type: 'password',
- }, 'Adding credential data fail.');
- //Delete entry
- var res = await req({
- url: '/records/1/credentials/6',
- method: 'delete'
- });
- assert.equal(res.status, 204, 'Deletion of entry should succeed for user.');
- //Delete entry without permission
- var res = await req({
- url: '/records/4/credentials/2',
- method: 'delete'
- });
- assert.equal(res.status, 403, 'Deletion of entry without permission should fail.');
- });
- });
|