1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace Tests\Controllers;
9
10use App\Models\ApiKey;
11use App\Models\User;
12use Tests\TestCase;
13
14class LegacyApiKeyControllerTest extends TestCase
15{
16 public function testDestroy(): void
17 {
18 $apiKey = ApiKey::factory()->create();
19 $user = $apiKey->user;
20
21 $this->expectCountChange(fn () => $user->apiKeys()->available()->count(), -1);
22
23 $this
24 ->actingAsVerified($user)
25 ->delete(route('legacy-api-key.destroy'))
26 ->assertSuccessful();
27 }
28
29 public function testDestroyWithoutExisting(): void
30 {
31 $user = User::factory()->create();
32
33 $this->expectCountChange(fn () => $user->apiKeys()->available()->count(), 0);
34
35 $this
36 ->actingAsVerified($user)
37 ->delete(route('legacy-api-key.destroy'))
38 ->assertSuccessful();
39 }
40
41 public function testDestroyGuest(): void
42 {
43 $this->delete(route('legacy-api-key.destroy'))->assertStatus(401);
44 }
45
46 public function testStore(): void
47 {
48 $user = User::factory()->create();
49
50 $this->expectCountChange(fn () => $user->apiKeys()->available()->count(), 1);
51
52 $appName = 'test app';
53 $appUrl = 'http://localhost';
54 $this
55 ->actingAsVerified($user)
56 ->post(route('legacy-api-key.store'), ['legacy_api_key' => [
57 'app_name' => 'test app',
58 'app_url' => 'http://localhost',
59 ]])->assertSuccessful()
60 ->assertJsonFragment([
61 'app_name' => $appName,
62 'app_url' => $appUrl,
63 ]);
64 }
65
66 /**
67 * @dataProvider dataProviderForStoreWithInvalidParams
68 */
69 public function testStoreWithInvalidParams($params): void
70 {
71 $user = User::factory()->create();
72
73 $this->expectCountChange(fn () => $user->apiKeys()->available()->count(), 0);
74
75 $this
76 ->actingAsVerified($user)
77 ->post(route('legacy-api-key.store'), ['legacy_api_key' => $params])
78 ->assertStatus(422);
79 }
80
81 public function testStoreWithExisting(): void
82 {
83 $apiKey = ApiKey::factory()->create();
84 $user = $apiKey->user;
85
86 $this->expectCountChange(fn () => $user->apiKeys()->available()->count(), 0);
87
88 $this
89 ->actingAsVerified($user)
90 ->post(route('legacy-api-key.store'), ['legacy_api_key' => [
91 'app_name' => 'test app 2',
92 'app_url' => 'http://localhost/2',
93 ]])->assertStatus(422);
94 }
95
96 public function testStoreGuest(): void
97 {
98 $this->post(route('legacy-api-key.store'))->assertStatus(401);
99 }
100
101 public static function dataProviderForStoreWithInvalidParams(): array
102 {
103 return [
104 [[
105 'app_name' => 'app name',
106 'app_url' => '',
107 ]],
108 [[
109 'app_name' => 'app name',
110 ]],
111 [[
112 'app_name' => '',
113 'app_url' => 'http://localhost',
114 ]],
115 [[
116 'app_url' => 'http://localhost',
117 ]],
118 [[
119 'app_name' => 'app name',
120 'app_url' => 'localhost',
121 ]],
122 [[
123 'app_name' => '',
124 'app_url' => '',
125 ]],
126 [[]],
127 ];
128 }
129}