|
| 1 | +const { Fury } = require('fury'); |
| 2 | +const { expect } = require('../../chai'); |
| 3 | +const parse = require('../../../../lib/parser/oas/parseOauthFlowObject'); |
| 4 | +const Context = require('../../../../lib/context'); |
| 5 | + |
| 6 | +const { minim: namespace } = new Fury(); |
| 7 | + |
| 8 | +describe('Oauth Flow Object', () => { |
| 9 | + let context; |
| 10 | + beforeEach(() => { |
| 11 | + context = new Context(namespace); |
| 12 | + }); |
| 13 | + |
| 14 | + it('provides warning when oauth flow is not an object', () => { |
| 15 | + const oauthFlow = new namespace.elements.String(); |
| 16 | + |
| 17 | + const parseResult = parse(context, oauthFlow); |
| 18 | + |
| 19 | + expect(parseResult.length).to.equal(1); |
| 20 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' is not an object"); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('#scopes', () => { |
| 24 | + it('provides a warning when scopes does not exist', () => { |
| 25 | + const oauthFlow = new namespace.elements.Object({ |
| 26 | + }); |
| 27 | + |
| 28 | + const parseResult = parse(context, oauthFlow); |
| 29 | + |
| 30 | + expect(parseResult.length).to.equal(1); |
| 31 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' is missing required property 'scopes'"); |
| 32 | + }); |
| 33 | + |
| 34 | + it('provides a warning when scopes is not an object', () => { |
| 35 | + const oauthFlow = new namespace.elements.Object({ |
| 36 | + scopes: 1, |
| 37 | + }); |
| 38 | + |
| 39 | + const parseResult = parse(context, oauthFlow); |
| 40 | + |
| 41 | + expect(parseResult.length).to.equal(1); |
| 42 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' 'scopes' is not an object"); |
| 43 | + }); |
| 44 | + |
| 45 | + it('provides a warning when scopes value item is not a string', () => { |
| 46 | + const oauthFlow = new namespace.elements.Object({ |
| 47 | + scopes: { |
| 48 | + read: 1, |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + const parseResult = parse(context, oauthFlow); |
| 53 | + |
| 54 | + expect(parseResult.length).to.equal(2); |
| 55 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' 'scopes' 'read' is not a string"); |
| 56 | + |
| 57 | + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Object); |
| 58 | + expect(parseResult.get(0).get('scopes')).to.be.instanceof(namespace.elements.Array); |
| 59 | + expect(parseResult.get(0).get('scopes').length).to.equal(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('parses scopes correctly', () => { |
| 63 | + const oauthFlow = new namespace.elements.Object({ |
| 64 | + scopes: { |
| 65 | + read: 'description', |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + const parseResult = parse(context, oauthFlow); |
| 70 | + |
| 71 | + expect(parseResult.length).to.equal(1); |
| 72 | + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Object); |
| 73 | + |
| 74 | + const scopes = parseResult.get(0).get('scopes'); |
| 75 | + |
| 76 | + expect(scopes).to.be.instanceof(namespace.elements.Array); |
| 77 | + expect(scopes.length).to.equal(1); |
| 78 | + |
| 79 | + expect(scopes.get(0)).to.be.instanceof(namespace.elements.String); |
| 80 | + expect(scopes.get(0).toValue()).to.equal('read'); |
| 81 | + expect(scopes.get(0).description).to.not.be.undefined; |
| 82 | + expect(scopes.get(0).description.toValue()).to.equal('description'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('#refreshUrl', () => { |
| 87 | + it('provides an warning when refreshUrl is not a string', () => { |
| 88 | + const oauthFlow = new namespace.elements.Object({ |
| 89 | + scopes: {}, |
| 90 | + refreshUrl: 1, |
| 91 | + }); |
| 92 | + |
| 93 | + const parseResult = parse(context, oauthFlow); |
| 94 | + |
| 95 | + expect(parseResult.length).to.equal(2); |
| 96 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' 'refreshUrl' is not a string"); |
| 97 | + |
| 98 | + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Object); |
| 99 | + expect(parseResult.get(0).get('scopes')).to.be.instanceof(namespace.elements.Array); |
| 100 | + expect(parseResult.get(0).get('scopes').length).to.equal(0); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('#tokenUrl', () => { |
| 105 | + it('provides an warning when tokenUrl is not a string', () => { |
| 106 | + const oauthFlow = new namespace.elements.Object({ |
| 107 | + scopes: {}, |
| 108 | + tokenUrl: 1, |
| 109 | + }); |
| 110 | + |
| 111 | + const parseResult = parse(context, oauthFlow); |
| 112 | + |
| 113 | + expect(parseResult.length).to.equal(2); |
| 114 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' 'tokenUrl' is not a string"); |
| 115 | + |
| 116 | + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Object); |
| 117 | + expect(parseResult.get(0).get('scopes')).to.be.instanceof(namespace.elements.Array); |
| 118 | + expect(parseResult.get(0).get('scopes').length).to.equal(0); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('#authorizationUrl', () => { |
| 123 | + it('provides an warning when authorizationUrl is not a string', () => { |
| 124 | + const oauthFlow = new namespace.elements.Object({ |
| 125 | + scopes: {}, |
| 126 | + authorizationUrl: 1, |
| 127 | + }); |
| 128 | + |
| 129 | + const parseResult = parse(context, oauthFlow); |
| 130 | + |
| 131 | + expect(parseResult.length).to.equal(2); |
| 132 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' 'authorizationUrl' is not a string"); |
| 133 | + |
| 134 | + expect(parseResult.get(0)).to.be.instanceof(namespace.elements.Object); |
| 135 | + expect(parseResult.get(0).get('scopes')).to.be.instanceof(namespace.elements.Array); |
| 136 | + expect(parseResult.get(0).get('scopes').length).to.equal(0); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('provides warning for invalid keys', () => { |
| 141 | + const oauthFlow = new namespace.elements.Object({ |
| 142 | + scopes: {}, |
| 143 | + invalid: '', |
| 144 | + }); |
| 145 | + |
| 146 | + const parseResult = parse(context, oauthFlow); |
| 147 | + |
| 148 | + expect(parseResult.length).to.equal(2); |
| 149 | + expect(parseResult).to.contain.warning("'Oauth Flow Object' contains invalid key 'invalid'"); |
| 150 | + }); |
| 151 | + |
| 152 | + it('does not provide warning/errors for extensions', () => { |
| 153 | + const oauthFlow = new namespace.elements.Object({ |
| 154 | + scopes: {}, |
| 155 | + 'x-extension': '', |
| 156 | + }); |
| 157 | + |
| 158 | + const parseResult = parse(context, oauthFlow); |
| 159 | + |
| 160 | + expect(parseResult).to.not.contain.annotations; |
| 161 | + }); |
| 162 | +}); |
0 commit comments