capabilities_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const Capabilities = require('../../lib/capabilities').Capabilities;
  19. const Symbols = require('../../lib/symbols');
  20. const assert = require('assert');
  21. describe('Capabilities', function() {
  22. it('can set and unset a capability', function() {
  23. let caps = new Capabilities();
  24. assert.equal(undefined, caps.get('foo'));
  25. assert.ok(!caps.has('foo'));
  26. caps.set('foo', 'bar');
  27. assert.equal('bar', caps.get('foo'));
  28. assert.ok(caps.has('foo'));
  29. caps.set('foo', null);
  30. assert.equal(null, caps.get('foo'));
  31. assert.ok(caps.has('foo'));
  32. });
  33. it('requires string capability keys', function() {
  34. let caps = new Capabilities();
  35. assert.throws(() => caps.set({}, 'hi'));
  36. });
  37. it('can merge capabilities', function() {
  38. let caps1 = new Capabilities()
  39. .set('foo', 'bar')
  40. .set('color', 'red');
  41. let caps2 = new Capabilities()
  42. .set('color', 'green');
  43. assert.equal('bar', caps1.get('foo'));
  44. assert.equal('red', caps1.get('color'));
  45. assert.equal('green', caps2.get('color'));
  46. assert.equal(undefined, caps2.get('foo'));
  47. caps2.merge(caps1);
  48. assert.equal('bar', caps1.get('foo'));
  49. assert.equal('red', caps1.get('color'));
  50. assert.equal('red', caps2.get('color'));
  51. assert.equal('bar', caps2.get('foo'));
  52. });
  53. it('can be initialized from a hash object', function() {
  54. let caps = new Capabilities({'one': 123, 'abc': 'def'});
  55. assert.equal(123, caps.get('one'));
  56. assert.equal('def', caps.get('abc'));
  57. });
  58. it('can be initialized from a map', function() {
  59. let m = new Map([['one', 123], ['abc', 'def']]);
  60. let caps = new Capabilities(m);
  61. assert.equal(123, caps.get('one'));
  62. assert.equal('def', caps.get('abc'));
  63. });
  64. describe('serialize', function() {
  65. it('works for simple capabilities', function() {
  66. let m = new Map([['one', 123], ['abc', 'def']]);
  67. let caps = new Capabilities(m);
  68. assert.deepEqual({one: 123, abc: 'def'}, caps[Symbols.serialize]());
  69. });
  70. it('does not omit capabilities set to a false-like value', function() {
  71. let caps = new Capabilities;
  72. caps.set('bool', false);
  73. caps.set('number', 0);
  74. caps.set('string', '');
  75. assert.deepEqual(
  76. {bool: false, number: 0, string: ''},
  77. caps[Symbols.serialize]());
  78. });
  79. it('omits capabilities with a null value', function() {
  80. let caps = new Capabilities;
  81. caps.set('foo', null);
  82. caps.set('bar', 123);
  83. assert.deepEqual({bar: 123}, caps[Symbols.serialize]());
  84. });
  85. it('omits capabilities with an undefined value', function() {
  86. let caps = new Capabilities;
  87. caps.set('foo', undefined);
  88. caps.set('bar', 123);
  89. assert.deepEqual({bar: 123}, caps[Symbols.serialize]());
  90. });
  91. });
  92. });