|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'tempfile' |
| 5 | + |
| 6 | +RSpec.describe Deploy::Configuration do |
| 7 | + describe '#websocket_hostname' do |
| 8 | + it 'has a default value' do |
| 9 | + config = described_class.new |
| 10 | + expect(config.websocket_hostname).to eq('wss://websocket.deployhq.com') |
| 11 | + end |
| 12 | + |
| 13 | + it 'allows setting a custom value' do |
| 14 | + config = described_class.new |
| 15 | + config.websocket_hostname = 'wss://custom.example.com' |
| 16 | + expect(config.websocket_hostname).to eq('wss://custom.example.com') |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + describe 'attribute setters and getters' do |
| 21 | + it 'sets and retrieves all configuration attributes' do |
| 22 | + config = described_class.new |
| 23 | + config.account = 'https://test.deployhq.com' |
| 24 | + config.username = 'testuser' |
| 25 | + config.api_key = 'test-api-key' |
| 26 | + config.project = 'test-project' |
| 27 | + |
| 28 | + expect(config.account).to eq('https://test.deployhq.com') |
| 29 | + expect(config.username).to eq('testuser') |
| 30 | + expect(config.api_key).to eq('test-api-key') |
| 31 | + expect(config.project).to eq('test-project') |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + describe '.from_file' do |
| 36 | + context 'with all fields present' do |
| 37 | + it 'loads configuration from JSON file' do |
| 38 | + config_data = { |
| 39 | + 'account' => 'https://test.deployhq.com', |
| 40 | + 'username' => 'testuser', |
| 41 | + 'api_key' => 'test-key', |
| 42 | + 'project' => 'test-project', |
| 43 | + 'websocket_hostname' => 'wss://test.example.com' |
| 44 | + } |
| 45 | + |
| 46 | + Tempfile.create(['config', '.json']) do |f| |
| 47 | + f.write(JSON.generate(config_data)) |
| 48 | + f.flush |
| 49 | + f.rewind |
| 50 | + |
| 51 | + config = described_class.from_file(f.path) |
| 52 | + |
| 53 | + expect(config.account).to eq('https://test.deployhq.com') |
| 54 | + expect(config.username).to eq('testuser') |
| 55 | + expect(config.api_key).to eq('test-key') |
| 56 | + expect(config.project).to eq('test-project') |
| 57 | + expect(config.websocket_hostname).to eq('wss://test.example.com') |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context 'without websocket_hostname' do |
| 63 | + it 'uses the default websocket hostname' do |
| 64 | + config_data = { |
| 65 | + 'account' => 'https://test.deployhq.com', |
| 66 | + 'username' => 'testuser', |
| 67 | + 'api_key' => 'test-key', |
| 68 | + 'project' => 'test-project' |
| 69 | + } |
| 70 | + |
| 71 | + Tempfile.create(['config', '.json']) do |f| |
| 72 | + f.write(JSON.generate(config_data)) |
| 73 | + f.flush |
| 74 | + f.rewind |
| 75 | + |
| 76 | + config = described_class.from_file(f.path) |
| 77 | + |
| 78 | + expect(config.account).to eq('https://test.deployhq.com') |
| 79 | + expect(config.websocket_hostname).to eq('wss://websocket.deployhq.com') |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context 'with missing file' do |
| 85 | + it 'raises Errno::ENOENT' do |
| 86 | + expect do |
| 87 | + described_class.from_file('/nonexistent/file.json') |
| 88 | + end.to raise_error(Errno::ENOENT) |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + context 'with invalid JSON' do |
| 93 | + it 'raises JSON::ParserError' do |
| 94 | + Tempfile.create(['config', '.json']) do |f| |
| 95 | + f.write('invalid json {') |
| 96 | + f.flush |
| 97 | + f.rewind |
| 98 | + |
| 99 | + expect do |
| 100 | + described_class.from_file(f.path) |
| 101 | + end.to raise_error(JSON::ParserError) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | +end |
0 commit comments