Skip to content

Commit 3795dd0

Browse files
committed
Add POST endpoint for Wiki profiles
1 parent 66e2ee1 commit 3795dd0

3 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Helper\ProfileValidator;
6+
use App\Wiki;
7+
use App\WikiProfile;
8+
use Illuminate\Http\Request;
9+
10+
class WikiProfileController extends Controller
11+
{
12+
private $profileValidator;
13+
14+
public function __construct(ProfileValidator $profileValidator)
15+
{
16+
$this->profileValidator = $profileValidator;
17+
}
18+
19+
public function create(Request $request): \Illuminate\Http\JsonResponse
20+
{
21+
$validatedInput = $request->validate([
22+
'wiki' => ['required', 'integer'],
23+
'profile' => ['required', 'json']
24+
]);
25+
26+
$wiki = Wiki::find($validatedInput['wiki']);
27+
if (!$wiki) {
28+
abort(404, 'No such wiki');
29+
}
30+
31+
$rawProfile = json_decode($validatedInput['profile'], true);
32+
$profileValidator = $this->profileValidator->validate($rawProfile);
33+
$profileValidator->validateWithBag('post');
34+
35+
$profile = WikiProfile::create(['wiki_id' => $wiki->id, ...$rawProfile]);
36+
return response()->json(['data' => $profile]);
37+
}
38+
}

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
$router->post('managers/list', ['uses' => 'WikiManagersController@getManagersOfWiki']);
4747
$router->get('entityImport', ['middleware' => 'limit_wiki_access', 'uses' => 'WikiEntityImportController@get']);
4848
$router->post('entityImport', ['middleware' => 'limit_wiki_access', 'uses' => 'WikiEntityImportController@create']);
49+
$router->post('profile', ['middleware' => 'limit_wiki_access', 'uses' => 'WikiProfileController@create']);
4950
});
5051
$router->apiResource('deletedWikiMetrics', 'DeletedWikiMetricsController')->only(['index'])
5152
->middleware(AuthorisedUsersForDeletedWikiMetricsMiddleware::class);
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
namespace Tests\Routes\Wiki\Managers;
4+
5+
use App\User;
6+
use App\Wiki;
7+
use App\WikiManager;
8+
use App\WikiProfile;
9+
use Tests\TestCase;
10+
11+
class ProfileControllerTest extends TestCase
12+
{
13+
protected $route = 'wiki/profile';
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
Wiki::query()->delete();
19+
WikiManager::query()->delete();
20+
WikiProfile::query()->delete();
21+
}
22+
23+
public function tearDown(): void
24+
{
25+
Wiki::query()->delete();
26+
WikiManager::query()->delete();
27+
WikiProfile::query()->delete();
28+
parent::tearDown();
29+
}
30+
31+
public function testFailOnMissingWiki(): void
32+
{
33+
$wiki = Wiki::factory()->create();
34+
$user = User::factory()->create(['verified' => true]);
35+
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
36+
$wiki->delete();
37+
38+
$this->actingAs($user, 'api')
39+
->json(
40+
'POST',
41+
$this->route,
42+
[
43+
'wiki' => $wiki->id,
44+
'profile' => json_encode([
45+
'audience' => 'wide',
46+
'temporality' => 'permanent',
47+
'purpose' => 'data_hub'
48+
])
49+
]
50+
)
51+
->assertStatus(404);
52+
}
53+
54+
public function testFailOnWrongWikiManager(): void
55+
{
56+
$userWiki = Wiki::factory()->create();
57+
$otherWiki = Wiki::factory()->create();
58+
$user = User::factory()->create(['verified' => true]);
59+
WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]);
60+
61+
$this->actingAs($user, 'api')
62+
->json(
63+
'POST',
64+
$this->route,
65+
[
66+
'wiki' => $otherWiki->id,
67+
'profile' => json_encode([
68+
'audience' => 'wide',
69+
'temporality' => 'permanent',
70+
'purpose' => 'data_hub'
71+
])
72+
]
73+
)
74+
->assertStatus(403);
75+
}
76+
77+
public function testFailOnInvalidProfile(): void
78+
{
79+
$wiki = Wiki::factory()->create();
80+
$user = User::factory()->create(['verified' => true]);
81+
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
82+
83+
$this->actingAs($user, 'api')
84+
->json(
85+
'POST',
86+
$this->route,
87+
[
88+
'wiki' => $wiki->id,
89+
'profile' => json_encode([
90+
'audience' => 'invalid option',
91+
'temporality' => 'permanent',
92+
'purpose' => 'data_hub'
93+
])
94+
]
95+
)
96+
->assertStatus(422)
97+
->assertJson([
98+
'message' => 'The selected audience is invalid.',
99+
'errors' => [
100+
'audience' => [
101+
'The selected audience is invalid.'
102+
]
103+
]
104+
]);
105+
}
106+
107+
public function testKeepAllVersions(): void
108+
{
109+
$wiki = Wiki::factory()->create();
110+
$user = User::factory()->create(['verified' => true]);
111+
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
112+
113+
$versionA = $this->actingAs($user, 'api')
114+
->json(
115+
'POST',
116+
$this->route,
117+
[
118+
'wiki' => $wiki->id,
119+
'profile' => json_encode([
120+
'audience' => 'wide',
121+
'temporality' => 'permanent',
122+
'purpose' => 'data_hub'
123+
])
124+
]
125+
)
126+
->assertStatus(200);
127+
128+
$versionB = $this->actingAs($user, 'api')
129+
->json(
130+
'POST',
131+
$this->route,
132+
[
133+
'wiki' => $wiki->id,
134+
'profile' => json_encode([
135+
'audience' => 'wide',
136+
'temporality' => 'temporary',
137+
'purpose' => 'data_hub'
138+
])
139+
]
140+
)
141+
->assertStatus(200);
142+
143+
$this->assertEquals(
144+
2,
145+
WikiProfile::where(['wiki_id' => $wiki->id])->count()
146+
);
147+
$this->assertEquals(
148+
'permanent',
149+
WikiProfile::find($versionA->decodeResponseJson()['data']['id'])['temporality']
150+
);
151+
$this->assertEquals(
152+
'temporary',
153+
WikiProfile::find($versionB->decodeResponseJson()['data']['id'])['temporality']
154+
);
155+
}
156+
}

0 commit comments

Comments
 (0)