|
1 | 1 | import { expect } from 'chai'; |
2 | | -import crypto, { randomUUID } from 'crypto'; |
| 2 | +import crypto, { randomInt, randomUUID } from 'crypto'; |
3 | 3 | import Sinon, { SinonSandbox } from 'sinon'; |
4 | 4 | import fs from 'fs/promises'; |
5 | 5 | import { ConfigService } from '../../src/services/config.service'; |
6 | 6 | import { CryptoService } from '../../src/services/crypto.service'; |
7 | | -import { CLICredentials, LoginCredentials } from '../../src/types/command.types'; |
| 7 | +import { CLICredentials, LoginCredentials, WebdavConfig } from '../../src/types/command.types'; |
8 | 8 | import { UserFixture } from '../fixtures/auth.fixture'; |
9 | 9 |
|
10 | 10 | import { config } from 'dotenv'; |
@@ -154,4 +154,66 @@ describe('Config service', () => { |
154 | 154 |
|
155 | 155 | expect(stubMkdir).to.be.calledOnceWith(ConfigService.WEBDAV_SSL_CERTS_DIR); |
156 | 156 | }); |
| 157 | + |
| 158 | + it('When webdav config options are saved, then they are written to a file', async () => { |
| 159 | + const webdavConfig: WebdavConfig = { |
| 160 | + port: String(randomInt(65000)), |
| 161 | + protocol: 'https', |
| 162 | + }; |
| 163 | + const stringConfig = JSON.stringify(webdavConfig); |
| 164 | + |
| 165 | + const fsStub = configServiceSandbox |
| 166 | + .stub(fs, 'writeFile') |
| 167 | + .withArgs(ConfigService.WEBDAV_CONFIGS_FILE, stringConfig) |
| 168 | + .resolves(); |
| 169 | + |
| 170 | + await ConfigService.instance.saveWebdavConfig(webdavConfig); |
| 171 | + expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE, stringConfig); |
| 172 | + }); |
| 173 | + |
| 174 | + it('When webdav config options are read and exist, then they are read from a file', async () => { |
| 175 | + const webdavConfig: WebdavConfig = { |
| 176 | + port: String(randomInt(65000)), |
| 177 | + protocol: 'http', |
| 178 | + }; |
| 179 | + const stringConfig = JSON.stringify(webdavConfig); |
| 180 | + |
| 181 | + const fsStub = configServiceSandbox |
| 182 | + .stub(fs, 'readFile') |
| 183 | + .withArgs(ConfigService.WEBDAV_CONFIGS_FILE) |
| 184 | + .resolves(stringConfig); |
| 185 | + |
| 186 | + const webdavConfigResult = await ConfigService.instance.readWebdavConfig(); |
| 187 | + expect(webdavConfigResult).to.be.eql(webdavConfig); |
| 188 | + expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE); |
| 189 | + }); |
| 190 | + |
| 191 | + it('When webdav config options are read but not exist, then they are returned from defaults', async () => { |
| 192 | + const defaultWebdavConfig: WebdavConfig = { |
| 193 | + port: ConfigService.WEBDAV_DEFAULT_PORT, |
| 194 | + protocol: ConfigService.WEBDAV_DEFAULT_PROTOCOL, |
| 195 | + }; |
| 196 | + |
| 197 | + const fsStub = configServiceSandbox |
| 198 | + .stub(fs, 'readFile') |
| 199 | + .withArgs(ConfigService.WEBDAV_CONFIGS_FILE) |
| 200 | + .resolves(undefined); |
| 201 | + |
| 202 | + const webdavConfigResult = await ConfigService.instance.readWebdavConfig(); |
| 203 | + expect(webdavConfigResult).to.be.eql(defaultWebdavConfig); |
| 204 | + expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE); |
| 205 | + }); |
| 206 | + |
| 207 | + it('When webdav config options are read but an error is thrown, then they are returned from defaults', async () => { |
| 208 | + const defaultWebdavConfig: WebdavConfig = { |
| 209 | + port: ConfigService.WEBDAV_DEFAULT_PORT, |
| 210 | + protocol: ConfigService.WEBDAV_DEFAULT_PROTOCOL, |
| 211 | + }; |
| 212 | + |
| 213 | + const fsStub = configServiceSandbox.stub(fs, 'readFile').withArgs(ConfigService.WEBDAV_CONFIGS_FILE).rejects(); |
| 214 | + |
| 215 | + const webdavConfigResult = await ConfigService.instance.readWebdavConfig(); |
| 216 | + expect(webdavConfigResult).to.be.eql(defaultWebdavConfig); |
| 217 | + expect(fsStub).to.be.calledWith(ConfigService.WEBDAV_CONFIGS_FILE); |
| 218 | + }); |
157 | 219 | }); |
0 commit comments