Skip to content

Commit 66daaf0

Browse files
author
unknown
committedOct 24, 2019
Change middleware. Update tests.
1 parent d971a9f commit 66daaf0

8 files changed

+43
-51
lines changed
 

‎app/Http/Middleware/IsAdmin.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ class IsAdmin
1515
*/
1616
public function handle($request, Closure $next)
1717
{
18-
if (auth()->user() && auth()->user()->hasRole('admin')) {
18+
$user = auth()->user();
19+
20+
if(!$user)
21+
return response()->json(['error'=>'Unauthorized'], 401);
22+
23+
if ($user->hasRole('admin')) {
1924
return $next($request);
2025
}
2126

22-
return response()->json(['error'=>'Unauthorized'], 403);
27+
return response()->json(['error'=>'Not admin'], 403);
2328
}
2429
}

‎app/Http/Middleware/IsAdvertiser.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ public function handle($request, Closure $next)
1717
{
1818
$user = auth()->user();
1919

20-
if ($user && ($user->hasRole('advertiser') || $user->isAdmin()) ) {
20+
if(!$user)
21+
return response()->json(['error'=>'Unauthorized'], 401);
22+
23+
if ($user->hasRole('advertiser') || $user->isAdmin()) {
2124
return $next($request);
2225
}
2326

24-
return response()->json(['error'=>'Unauthorized'], 403);
27+
return response()->json(['error'=>'Not advertiser'], 403);
2528
}
2629
}

‎app/Http/Middleware/IsStreamer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ public function handle($request, Closure $next)
1717
{
1818
$user = auth()->user();
1919

20-
if ($user && ($user->hasRole('streamer') || $user->isAdmin()) ) {
20+
if(!$user)
21+
return response()->json(['error'=>'Unauthorized'], 401);
22+
23+
if ($user->hasRole('streamer') || $user->isAdmin()) {
2124
return $next($request);
2225
}
2326

24-
return response()->json(['error'=>'Unauthorized'], 403);
27+
return response()->json(['error'=>'Not streamer'], 403);
2528
}
2629
}

‎tests/Feature/AdvCampaignTest.php

+21-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ class AdvCampaignTest extends TestCase
1212
{
1313
use RefreshDatabase;
1414

15-
protected function setUp(): void
16-
{
17-
parent::setUp();
18-
19-
$this->generateRoles();
20-
}
21-
2215
/** @test */
2316
public function not_auth_user_cannot_create_it()
2417
{
@@ -41,7 +34,7 @@ public function auth_user_not_advertiser_cannot_create_campaign()
4134

4235
$campaign = factory(AdvCampaign::class)->make();
4336

44-
$this->storeAssertFieldFailed($campaign->toArray(), $token, 'title', 401, false);
37+
$this->storeAssertFieldFailed($campaign->toArray(), $token, 'title', 403, false);
4538

4639
auth()->logout();
4740
}
@@ -89,10 +82,29 @@ public function auth_user_create_but_wrong_dates()
8982
$this->storeAssertFieldFailed($data, $token, 'from');
9083
}
9184

85+
/** @test */
86+
public function auth_user_create_not_enough_money()
87+
{
88+
$user = factory(User::class)->create(['role_id' => 4]);
89+
$user->account->update(['amount' => 50]);
90+
$token = auth()->login($user);
91+
92+
$data = [
93+
'title' => "Updated stream",
94+
'brand' => 'Brand',
95+
'limit' => 100,
96+
'from' => Carbon::now('UTC')->addMinutes(45)->toDateTimeString(),
97+
'to' => Carbon::now('UTC')->addMinutes(245)->toDateTimeString(),
98+
];
99+
100+
$this->storeAssertFieldFailed($data, $token, 'limit');
101+
}
102+
92103
/** @test */
93104
public function auth_user_create_successfully()
94105
{
95106
$user = factory(User::class)->create(['role_id' => 4]);
107+
$user->account->update(['amount' => 200]);
96108
$token = auth()->login($user);
97109

98110
$data = [
@@ -119,7 +131,7 @@ public function auth_user_role_user_cannot_watch_list_it()
119131
$token = auth()->login($user);
120132

121133
$this->json('GET', '/api/campaigns', [], ['Authorization' => "Bearer $token"])
122-
->assertStatus(401);
134+
->assertStatus(403);
123135
}
124136

125137
/** @test */

‎tests/Feature/AdvTaskTest.php

+2-36
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ class AdvTaskTest extends TestCase
1515
{
1616
use RefreshDatabase;
1717

18-
protected function setUp(): void
19-
{
20-
parent::setUp();
21-
22-
$this->generateRoles();
23-
}
24-
2518
/** @test */
2619
public function not_auth_user_cannot_create_it()
2720
{
@@ -49,7 +42,7 @@ public function auth_user_not_advertiser_cannot_create_it()
4942
$task = factory(AdvTask::class)->make(['campaign_id' => $campaign->id]);
5043

5144
$this->json('POST', '/api/campaigns/'.$campaign->id."/tasks", $task->toArray(), ['Authorization' => "Bearer $token"])
52-
->assertStatus(401);
45+
->assertStatus(403);
5346

5447
auth()->logout();
5548
}
@@ -92,33 +85,6 @@ public function auth_user_create_but_requires_fields_not_filled()
9285
}
9386
}
9487

95-
/** @test */
96-
public function auth_user_create_but_campaign_already_started()
97-
{
98-
$user = factory(User::class)->create(['role_id' => 4]);
99-
$campaign = factory(AdvCampaign::class)->create([
100-
'user_id' => $user->id,
101-
'from' => Carbon::now('UTC')->subMinutes(245)->toDateTimeString(),
102-
'to' => Carbon::now('UTC')->addMinutes(45)->toDateTimeString()
103-
]);
104-
$token = auth()->login($user);
105-
106-
$url = '/api/campaigns/'.$campaign->id."/tasks";
107-
108-
$data = [
109-
'campaign_id' => $campaign->id,
110-
'small_desc' => 'task',
111-
'full_desc' => 'full task',
112-
'limit' => 50,
113-
'type' => 1,
114-
'price' => 5,
115-
'min_rating' => 0
116-
];
117-
118-
$this->storeAssertFieldFailed($url, $data, $token, 'price');
119-
}
120-
121-
12288
/** @test */
12389
public function auth_user_create_successfully()
12490
{
@@ -158,7 +124,7 @@ public function auth_user_role_user_cannot_view_all()
158124
$token = auth()->login($user);
159125

160126
$this->json('GET', '/api/campaigns/all/tasks', [], ['Authorization' => "Bearer $token"])
161-
->assertStatus(401);
127+
->assertStatus(403);
162128
}
163129

164130
/** @test */

‎tests/Feature/ChannelTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function show_it_by_slug_and_id()
2626
$channel->refresh();
2727
$r = new ChannelResource($channel);
2828
$d = json_decode(json_encode($r->toResponse(app('request'))->getData()), true);
29+
$d = $d['data'];
2930

3031
$this->json('get', '/api/channels/'.$channel->id)
3132
->assertStatus(200)

‎tests/Feature/StreamTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ public function show_it_by_id()
373373
$stream->refresh();
374374
$r = new StreamResource($stream);
375375
$d = json_decode(json_encode($r->toResponse(app('request'))->getData()), true);
376+
$d = $d['data'];
376377

377378
$this->json('get', '/api/streams/'.$stream->id)
378379
->assertStatus(200)

‎tests/TestCase.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected function setUp(): void
1818

1919
Auth::shouldUse('api');
2020
JsonResource::wrap('data');
21+
$this->generateRoles();
2122
}
2223

2324
protected function generateRoles()

0 commit comments

Comments
 (0)
Please sign in to comment.