Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Aug 23, 2023
1 parent 6cd48fa commit 86b0104
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 156 deletions.
7 changes: 2 additions & 5 deletions app/Dtos/TikTokUserDto.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace App\Dtos;

class TikTokUserDto
Expand Down Expand Up @@ -60,11 +62,6 @@ public function getOpenId(): string
return $this->open_id;
}

public function isIsVerified(): bool
{
return $this->is_verified;
}

public function getProfileDeepLink(): string
{
return $this->profile_deep_link;
Expand Down
1 change: 0 additions & 1 deletion app/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

namespace App\Exceptions;

class ApiException extends \Exception
{
}
1 change: 0 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Illuminate\Validation\ValidationException;
use Throwable;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;

class Handler extends ExceptionHandler
{
Expand Down
20 changes: 2 additions & 18 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\JWT;

class CommentController extends Controller
{
public function addComment(Request $request, JWT $jwtAuth)
public function addComment(Request $request)
{
/** @phpstan-ignore-next-line */
if ($user = JWTAuth::parseToken()->user()) {
Expand Down Expand Up @@ -45,8 +44,6 @@ public function addComment(Request $request, JWT $jwtAuth)
}
}
}

throw new ApiException('You are not suthorized to perfrom this action.');
}

public function destroyComment(Request $request)
Expand All @@ -57,23 +54,10 @@ public function destroyComment(Request $request)
$comment = Comment::findOrFail($request->only(['comment-id']))->first();

if ($user->isSuper() || $user->ownsComment($payload['comment-id'])) {
try {
return response()->json(['deleted' => $comment->delete()]);
} catch (\Exception $exception) {
Log::debug(
'comment deletion failed.',
['error' => $exception, 'payload' => $payload]
);

return response()->json([
'error' => 'There was an error processing this request. Please try again later.'
], 400);
}
return response()->json(['deleted' => $comment->delete()]);
} else {
throw new ApiException('You are not suthorized to perfrom this action.');
}
}

throw new ApiException('You are not suthorized to perfrom this action.');
}
}
77 changes: 0 additions & 77 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,45 @@
use App\Exceptions\ApiException;
use App\Http\Requests\UserStoreRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Jobs\TriggerEmailVerificationProcess;
use App\Mail\OtpWasGenerated;
use App\Models\EmailVerification;
use App\Models\Following;
use App\Models\User;
use App\Models\UserFeedback;
use App\Services\TikTok\AccessToken;
use App\Services\TikTok\HttpRequestRunner;
use App\Services\TikTok\Videos;
use App\Services\UserService;
use Carbon\Carbon;
use Ichtrojan\Otp\Otp;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Tymon\JWTAuth\Facades\JWTAuth;
use Ichtrojan\Otp\Models\Otp as OtpModel;

/**
* Class UserController
*/
class UserController extends Controller
{
protected UserService $service;

/**
* @param \App\Services\UserService $service
*/
public function __construct(UserService $service)
{
$this->service = $service;
}

/**
* Get all users from the database
*/
public function index()
{
return $this->service->index();
}

/**
* @param UserStoreRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(UserStoreRequest $request): \Illuminate\Http\JsonResponse
{
return $this->service->store($request);
}

/**
* Get one user
*
* @param mixed $username username
* @throws \App\Exceptions\CookbookModelNotFoundException
*/
public function show($username)
{
return $this->service->show($username);
}

/**
* @param $username
* @param UserUpdateRequest $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|Response
*/
public function update($username, UserUpdateRequest $request)
{
if ($request->all()) {
Expand All @@ -88,54 +59,6 @@ public function update($username, UserUpdateRequest $request)
]);
}

/**
* Email Verification
*
* @param $token
* @return \Illuminate\Http\JsonResponse|void
*/
public function verifyEmail($token)
{
$payload = Crypt::decrypt($token);

try {
if ($payload['secret'] != env('CRYPT_SECRET')) { //one more layer of scrutiny
Log::info('Invalid secret provided for verifying this email', ['user_id' => $payload['user_id'], 'email' => $payload['email']]);
throw new \Exception('There was a problem processing this request. Please try again later.');
}

$user = User::findOrFail($payload['user_id']);

if ($user) {
$verification = EmailVerification::where('user_id', $payload['user_id']);
$verification->update([
'is_verified' => Carbon::now(),
]);

return response()->json(null, Response::HTTP_NO_CONTENT);
}
} catch (\Exception $e) {
return response()->json($e->getMessage(), Response::HTTP_CONFLICT);
}
}

/**
* @param $token
*
* @throws \Exception
*/
public function resend($token)
{
$payload = Crypt::decrypt($token);

if ($payload['secret'] != env('CRYPT_SECRET')) { //one more layer of scrutiny
Log::info('Invalid secret provided for resending email verification', ['user_id' => $payload['user_id'], 'email' => $payload['email']]);
throw new \Exception('There was a problem processing this request. Please try again later.');
} else {
dispatch(new TriggerEmailVerificationProcess($payload['user_id']));
}
}

public function followUser(Request $request)
{
/** @phpstan-ignore-next-line */
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/UserUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UserUpdateRequest extends FormRequest
*/
public function authorize()
{
return false;
return true;
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/Jobs/SendEmailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class SendEmailNotification extends BaseNotification
{
const TYPE = 'email';
protected $tries;

/**
* SendEmailNotification constructor.
Expand Down
7 changes: 1 addition & 6 deletions app/Services/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Validation\UnauthorizedException;

/**
* Class RecipeService
Expand Down Expand Up @@ -179,8 +178,6 @@ public function update(Request $request, $id)
], Response::HTTP_OK
);
}

throw new UnauthorizedException("You are not authorized to perform this action.");
}

/**
Expand All @@ -200,8 +197,6 @@ public function delete(User $user, $id)
], Response::HTTP_ACCEPTED
);
}

throw new UnauthorizedException("You are not authorized to perform this action.");
}

/**
Expand Down Expand Up @@ -256,7 +251,7 @@ private function validatePayload(array $payload)
if ($cookbook_id = Arr::get($payload, 'cookbook_id')) {
if (!Cookbook::find($cookbook_id)) {
$sources[] = [
'cookbook_id' => $cookbook_id . ' does not exist.'
'cookbook_id' => 'This cookbook does not exist.'
];
}
}
Expand Down
Loading

0 comments on commit 86b0104

Please sign in to comment.