|
5 | 5 | use App\Models\Application; |
6 | 6 | use App\Models\GitlabApp; |
7 | 7 | use App\Models\InstanceSettings; |
| 8 | +use App\Models\PrivateKey; |
8 | 9 | use App\Models\Team; |
9 | 10 | use App\Models\User; |
10 | 11 | use Illuminate\Foundation\Testing\RefreshDatabase; |
11 | 12 | use Livewire\Livewire; |
12 | 13 |
|
13 | 14 | uses(RefreshDatabase::class); |
14 | 15 |
|
| 16 | +function gitlabTestPrivateKey(): string |
| 17 | +{ |
| 18 | + $key = openssl_pkey_new([ |
| 19 | + 'private_key_bits' => 2048, |
| 20 | + 'private_key_type' => OPENSSL_KEYTYPE_RSA, |
| 21 | + ]); |
| 22 | + |
| 23 | + openssl_pkey_export($key, $privateKey); |
| 24 | + |
| 25 | + return $privateKey; |
| 26 | +} |
| 27 | + |
15 | 28 | beforeEach(function () { |
16 | 29 | $this->team = Team::factory()->create(); |
17 | 30 | $this->owner = User::factory()->create(); |
|
104 | 117 | expect($this->gitlabApp->refresh()->is_system_wide)->toBeTrue(); |
105 | 118 | }); |
106 | 119 |
|
| 120 | + test('saving settings keeps key selection within the source team', function () { |
| 121 | + $otherTeam = Team::factory()->create(); |
| 122 | + $foreignKey = PrivateKey::create([ |
| 123 | + 'name' => 'other-team-key', |
| 124 | + 'private_key' => gitlabTestPrivateKey(), |
| 125 | + 'team_id' => $otherTeam->id, |
| 126 | + ]); |
| 127 | + $ownKey = PrivateKey::create([ |
| 128 | + 'name' => 'own-team-key', |
| 129 | + 'private_key' => gitlabTestPrivateKey(), |
| 130 | + 'team_id' => $this->team->id, |
| 131 | + ]); |
| 132 | + $this->gitlabApp->update([ |
| 133 | + 'api_url' => 'https://gitlab.com/api/v4', |
| 134 | + 'html_url' => 'https://gitlab.com', |
| 135 | + ]); |
| 136 | + |
| 137 | + $this->actingAs($this->owner); |
| 138 | + session(['currentTeam' => $this->team]); |
| 139 | + |
| 140 | + Livewire::withQueryParams(['gitlab_app_uuid' => $this->gitlabApp->uuid]) |
| 141 | + ->test(Change::class) |
| 142 | + ->set('privateKeyId', $foreignKey->id) |
| 143 | + ->call('submit'); |
| 144 | + |
| 145 | + expect($this->gitlabApp->refresh()->private_key_id)->toBeNull(); |
| 146 | + |
| 147 | + Livewire::withQueryParams(['gitlab_app_uuid' => $this->gitlabApp->uuid]) |
| 148 | + ->test(Change::class) |
| 149 | + ->set('privateKeyId', $ownKey->id) |
| 150 | + ->call('submit') |
| 151 | + ->assertHasNoErrors(); |
| 152 | + |
| 153 | + expect($this->gitlabApp->refresh()->private_key_id)->toBe($ownKey->id); |
| 154 | + }); |
| 155 | + |
107 | 156 | test('instantSave rejects unsafe GitLab URLs', function (string $url) { |
108 | 157 | $this->actingAs($this->owner); |
109 | 158 | session(['currentTeam' => $this->team]); |
|
0 commit comments