|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import sinon from 'sinon'; |
| 5 | +import ConnectionStringUrl from 'mongodb-connection-string-url'; |
| 6 | + |
| 7 | +import SSLTab, { getTLSOptionForConnectionString } from './tls-ssl-tab'; |
| 8 | + |
| 9 | +describe('SchemaInput', function () { |
| 10 | + let updateConnectionFormFieldSpy: sinon.SinonSpy; |
| 11 | + |
| 12 | + beforeEach(function () { |
| 13 | + updateConnectionFormFieldSpy = sinon.spy(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('with ssl=true', function () { |
| 17 | + beforeEach(function () { |
| 18 | + const connectionStringUrl = new ConnectionStringUrl( |
| 19 | + 'mongodb+srv://0ranges:p!neapp1es@localhost/?ssl=true' |
| 20 | + ); |
| 21 | + render( |
| 22 | + <SSLTab |
| 23 | + connectionStringUrl={connectionStringUrl} |
| 24 | + updateConnectionFormField={updateConnectionFormFieldSpy} |
| 25 | + /> |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should render the TLS/SSL `On` radio box selected', function () { |
| 30 | + const tlsOnRadioBox = screen.getAllByRole('radio')[1] as HTMLInputElement; |
| 31 | + expect(tlsOnRadioBox.checked).to.equal(true); |
| 32 | + expect(tlsOnRadioBox.getAttribute('aria-checked')).to.equal('true'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should render TLS/SSL `Default` and `Off` radio boxes not selected', function () { |
| 36 | + const tlsDefaultRadioBox = screen.getAllByRole( |
| 37 | + 'radio' |
| 38 | + )[0] as HTMLInputElement; |
| 39 | + expect(tlsDefaultRadioBox.checked).to.equal(false); |
| 40 | + expect(tlsDefaultRadioBox.getAttribute('aria-checked')).to.equal('false'); |
| 41 | + |
| 42 | + const tlsOffRadioBox = screen.getAllByRole( |
| 43 | + 'radio' |
| 44 | + )[2] as HTMLInputElement; |
| 45 | + expect(tlsOffRadioBox.checked).to.equal(false); |
| 46 | + expect(tlsOffRadioBox.getAttribute('aria-checked')).to.equal('false'); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('when TLS/SSL default is clicked', function () { |
| 50 | + beforeEach(function () { |
| 51 | + const tlsDefaultRadioBox = screen.getAllByRole('radio')[0]; |
| 52 | + fireEvent.click(tlsDefaultRadioBox); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should call to update the connection configuration to TLS/SSL default', function () { |
| 56 | + expect(updateConnectionFormFieldSpy.callCount).to.equal(1); |
| 57 | + expect(updateConnectionFormFieldSpy.firstCall.args[0]).to.deep.equal({ |
| 58 | + type: 'update-tls-option', |
| 59 | + tlsOption: 'DEFAULT', |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('when TLS/SSL off is clicked', function () { |
| 65 | + beforeEach(function () { |
| 66 | + const standardSchemaRadioBox = screen.getAllByRole('radio')[2]; |
| 67 | + fireEvent.click(standardSchemaRadioBox); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should call to update the connection configuration to TLS/SSL off', function () { |
| 71 | + expect(updateConnectionFormFieldSpy.callCount).to.equal(1); |
| 72 | + expect(updateConnectionFormFieldSpy.firstCall.args[0]).to.deep.equal({ |
| 73 | + type: 'update-tls-option', |
| 74 | + tlsOption: 'OFF', |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('when TLS/SSL on is clicked', function () { |
| 80 | + beforeEach(function () { |
| 81 | + const standardSchemaRadioBox = screen.getAllByRole('radio')[1]; |
| 82 | + fireEvent.click(standardSchemaRadioBox); |
| 83 | + }); |
| 84 | + |
| 85 | + it("shouldn't call to update anything", function () { |
| 86 | + expect(updateConnectionFormFieldSpy.callCount).to.equal(0); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('with ssl=false', function () { |
| 92 | + beforeEach(function () { |
| 93 | + const connectionStringUrl = new ConnectionStringUrl( |
| 94 | + 'mongodb+srv://0ranges:p!neapp1es@localhost/?ssl=false' |
| 95 | + ); |
| 96 | + render( |
| 97 | + <SSLTab |
| 98 | + connectionStringUrl={connectionStringUrl} |
| 99 | + updateConnectionFormField={updateConnectionFormFieldSpy} |
| 100 | + /> |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should render the TLS/SSL `Off` radio box selected', function () { |
| 105 | + const tlsOnRadioBox = screen.getAllByRole('radio')[2] as HTMLInputElement; |
| 106 | + expect(tlsOnRadioBox.checked).to.equal(true); |
| 107 | + expect(tlsOnRadioBox.getAttribute('aria-checked')).to.equal('true'); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('when TLS/SSL off is clicked', function () { |
| 111 | + beforeEach(function () { |
| 112 | + const standardSchemaRadioBox = screen.getAllByRole('radio')[2]; |
| 113 | + fireEvent.click(standardSchemaRadioBox); |
| 114 | + }); |
| 115 | + |
| 116 | + it("shouldn't call to update anything", function () { |
| 117 | + expect(updateConnectionFormFieldSpy.callCount).to.equal(0); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('when TLS/SSL on is clicked', function () { |
| 122 | + beforeEach(function () { |
| 123 | + const standardSchemaRadioBox = screen.getAllByRole('radio')[1]; |
| 124 | + fireEvent.click(standardSchemaRadioBox); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should call to update the connection configuration to TLS/SSL on', function () { |
| 128 | + expect(updateConnectionFormFieldSpy.callCount).to.equal(1); |
| 129 | + expect(updateConnectionFormFieldSpy.firstCall.args[0]).to.deep.equal({ |
| 130 | + type: 'update-tls-option', |
| 131 | + tlsOption: 'ON', |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('#getTLSOptionForConnectionString', function () { |
| 138 | + let testUrl: ConnectionStringUrl; |
| 139 | + |
| 140 | + beforeEach(function () { |
| 141 | + testUrl = new ConnectionStringUrl('mongodb://localhost'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should return `DEFAULT` when tls and ssl are unset', function () { |
| 145 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('DEFAULT'); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('when tls is `true`', function () { |
| 149 | + beforeEach(function () { |
| 150 | + testUrl.searchParams.set('tls', 'true'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should return `ON`', function () { |
| 154 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('ON'); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('when ssl is `false`', function () { |
| 158 | + beforeEach(function () { |
| 159 | + testUrl.searchParams.set('ssl', 'false'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should return `undefined`', function () { |
| 163 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal(undefined); |
| 164 | + }); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe('when ssl is `true`', function () { |
| 169 | + beforeEach(function () { |
| 170 | + testUrl.searchParams.set('ssl', 'true'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should return `ON`', function () { |
| 174 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('ON'); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('when tls is `false`', function () { |
| 178 | + beforeEach(function () { |
| 179 | + testUrl.searchParams.set('tls', 'false'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should return `undefined`', function () { |
| 183 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal(undefined); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe('when tls is `true`', function () { |
| 188 | + beforeEach(function () { |
| 189 | + testUrl.searchParams.set('tls', 'true'); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should return `ON`', function () { |
| 193 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('ON'); |
| 194 | + }); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + describe('when ssl is `false`', function () { |
| 199 | + beforeEach(function () { |
| 200 | + testUrl.searchParams.set('ssl', 'false'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should return `OFF`', function () { |
| 204 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('OFF'); |
| 205 | + }); |
| 206 | + |
| 207 | + describe('when tls is `true`', function () { |
| 208 | + beforeEach(function () { |
| 209 | + testUrl.searchParams.set('tls', 'true'); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should return `undefined`', function () { |
| 213 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal(undefined); |
| 214 | + }); |
| 215 | + }); |
| 216 | + }); |
| 217 | + |
| 218 | + describe('when tls is `false`', function () { |
| 219 | + beforeEach(function () { |
| 220 | + testUrl.searchParams.set('tls', 'false'); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should return `ON`', function () { |
| 224 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('OFF'); |
| 225 | + }); |
| 226 | + |
| 227 | + describe('when ssl `false`', function () { |
| 228 | + beforeEach(function () { |
| 229 | + testUrl.searchParams.set('ssl', 'false'); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should return `OFF`', function () { |
| 233 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal('OFF'); |
| 234 | + }); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + describe('when tls has a value not `true` or `false`', function () { |
| 239 | + beforeEach(function () { |
| 240 | + testUrl.searchParams.set('ssl', 'aaaa'); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should return `undefined`', function () { |
| 244 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal(undefined); |
| 245 | + }); |
| 246 | + }); |
| 247 | + |
| 248 | + describe('when ssl has a value not `true` or `false`', function () { |
| 249 | + beforeEach(function () { |
| 250 | + testUrl.searchParams.set('ssl', 'aaaa'); |
| 251 | + }); |
| 252 | + |
| 253 | + it('should return `undefined`', function () { |
| 254 | + expect(getTLSOptionForConnectionString(testUrl)).to.equal(undefined); |
| 255 | + }); |
| 256 | + }); |
| 257 | + }); |
| 258 | +}); |
0 commit comments