From fec2f9c9210fc3c480d18fc1215c25e6e7e07aa9 Mon Sep 17 00:00:00 2001 From: ViNoS-ab Date: Mon, 4 Dec 2023 01:06:57 +0100 Subject: [PATCH 01/11] added team model, and get create update controllers and routes --- app/Http/Controllers/API/AdminController.php | 36 ++++++++++- app/Http/Middleware/EnsureTeamExists.php | 29 +++++++++ app/Http/Repositories/TeamRepository.php | 60 +++++++++++++++++++ app/Http/Resources/TeamResource.php | 22 +++++++ .../Resources/User/ParticipantResource.php | 1 + app/Models/Team.php | 20 +++++++ app/Models/User.php | 8 ++- routes/api.php | 7 +++ 8 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 app/Http/Middleware/EnsureTeamExists.php create mode 100644 app/Http/Repositories/TeamRepository.php create mode 100644 app/Http/Resources/TeamResource.php create mode 100644 app/Models/Team.php diff --git a/app/Http/Controllers/API/AdminController.php b/app/Http/Controllers/API/AdminController.php index 2f02ce2..b998ac5 100644 --- a/app/Http/Controllers/API/AdminController.php +++ b/app/Http/Controllers/API/AdminController.php @@ -6,6 +6,7 @@ use App\Http\Repositories\GeneralRepository; use App\Http\Repositories\UserRepository; use App\Http\Repositories\TrackRepository; +use App\Http\Repositories\TeamRepository; use Illuminate\Http\Request; class AdminController extends BaseController @@ -14,11 +15,14 @@ class AdminController extends BaseController private $challengeRepository; private $trackRepository; private $generalRepository; - public function __construct(UserRepository $userRepository, ChallengeRepository $challengeRepository, TrackRepository $trackRepository, GeneralRepository $generalRepository) { + private $teamRepository; + public function __construct(UserRepository $userRepository, ChallengeRepository $challengeRepository, TrackRepository $trackRepository, GeneralRepository $generalRepository, TeamRepository $teamRepository) + { $this->userRepository = $userRepository; $this->challengeRepository = $challengeRepository; $this->trackRepository = $trackRepository; $this->generalRepository = $generalRepository; + $this->teamRepository = $teamRepository; } public function get_all_users() { @@ -133,4 +137,34 @@ public function get_stats() { return $this->sendResponse($response['data'], $response['message']); } + public function get_teams() + { + $response = $this->teamRepository->getTeams(); + return $this->sendResponse($response['data'], $response['message']); + } + + public function get_team($id) + { + $response = $this->teamRepository->getTeam($id); + return $this->sendResponse($response['data'], $response['message']); + + } + + public function create_team(Request $request) + { + $response = $this->teamRepository->createTeam($request); + if (!$response['success']) + return $this->sendError($response['message'], $response['data']); + return $this->sendResponse($response['data'], $response['message']); + + } + + public function update_team(Request $request, $id) + { + $response = $this->teamRepository->updateTeam($request, $id); + if (!$response['success']) + return $this->sendError($response['message'], $response['data']); + return $this->sendResponse($response['data'], $response['message']); + + } } diff --git a/app/Http/Middleware/EnsureTeamExists.php b/app/Http/Middleware/EnsureTeamExists.php new file mode 100644 index 0000000..57a600d --- /dev/null +++ b/app/Http/Middleware/EnsureTeamExists.php @@ -0,0 +1,29 @@ +route('id')); + if (!$challenge) + return response()->json([ + 'success' => false, + 'message' => 'Team can not be found' + ], 400); + + return $next($request); + } +} diff --git a/app/Http/Repositories/TeamRepository.php b/app/Http/Repositories/TeamRepository.php new file mode 100644 index 0000000..63c1805 --- /dev/null +++ b/app/Http/Repositories/TeamRepository.php @@ -0,0 +1,60 @@ +all(), ['name' => 'required|unique:teams,name']); + + if ($validator->fails()) { + $response['success'] = false; + $response['message'] = 'Validation failed!'; + $response['data'] = $validator->errors(); + return $response; + } + $team = Team::create($request); + $response['success'] = true; + $response['data'] = $team; + $response['message'] = 'Successfully created the team!'; + return $response; + } + + public function updateTeam($request, $id) + { + $response = []; + $team = Team::find($id); + $team->name = $request->name; + $team->save(); + $response['success'] = true; + $response['data'] = $team; + $response['message'] = 'Successfully updated the team!'; + return $response; + } +} \ No newline at end of file diff --git a/app/Http/Resources/TeamResource.php b/app/Http/Resources/TeamResource.php new file mode 100644 index 0000000..ced8883 --- /dev/null +++ b/app/Http/Resources/TeamResource.php @@ -0,0 +1,22 @@ + $this->id, + 'name' => $this->name, + 'participants' => ParticipantResource::collection(User::where('team_id', $this->id)->get()->toArray()), + ]; + } + +} \ No newline at end of file diff --git a/app/Http/Resources/User/ParticipantResource.php b/app/Http/Resources/User/ParticipantResource.php index d6c496e..b547695 100644 --- a/app/Http/Resources/User/ParticipantResource.php +++ b/app/Http/Resources/User/ParticipantResource.php @@ -24,6 +24,7 @@ public function toArray($request) 'email_verified' => $this->email_verified_at ? true: false, 'track' => $this->track?->type, 'submissions' => SubmissionResource::collection($this->submissions), + 'team' => $this->team?->name, ]; } } diff --git a/app/Models/Team.php b/app/Models/Team.php new file mode 100644 index 0000000..e4b50a4 --- /dev/null +++ b/app/Models/Team.php @@ -0,0 +1,20 @@ +hasMany('App\Models\User'); + } +} + diff --git a/app/Models/User.php b/app/Models/User.php index cbf3db4..cc88e55 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -25,7 +25,8 @@ class User extends Authenticatable implements MustVerifyEmail 'points', 'role', 'track_id', - 'ip' + 'ip', + 'team_id' ]; @@ -78,5 +79,10 @@ public function username() { return "full_name"; } + public function team() + { + return $this->belongsTo('App\Models\Team'); + } + } diff --git a/routes/api.php b/routes/api.php index 776b232..c906137 100644 --- a/routes/api.php +++ b/routes/api.php @@ -60,6 +60,13 @@ Route::post('/{id}/unlock', 'unlock_track')->middleware('trackExists'); Route::delete('/{id}/delete', 'delete_track')->middleware('trackExists'); }); + + Route::prefix('team')->group(function () { + Route::get('/', 'get_teams'); + Route::post('/create', 'create_team'); + Route::post('/{id}/update', 'update_team')->middleware('teamExists'); + + }); }); Route::prefix('participant')->middleware(['auth:sanctum', 'hasRole:participant', 'verified']) From 7b8cd2f57eb8ec65b1bea36d70b0fc3b86125a4a Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Tue, 5 Dec 2023 02:46:28 +0100 Subject: [PATCH 02/11] added team remove & add&remove member routes --- app/Http/Controllers/API/AdminController.php | 26 +++++++++++++++ app/Http/Repositories/TeamRepository.php | 34 ++++++++++++++++++++ database/seeders/UserSeeder.php | 2 +- routes/api.php | 7 +++- 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/API/AdminController.php b/app/Http/Controllers/API/AdminController.php index b998ac5..541cac8 100644 --- a/app/Http/Controllers/API/AdminController.php +++ b/app/Http/Controllers/API/AdminController.php @@ -167,4 +167,30 @@ public function update_team(Request $request, $id) return $this->sendResponse($response['data'], $response['message']); } + public function delete_team($id) + { + $response = $this->teamRepository->deleteTeam($id); + if (!$response['success']) + return $this->sendError($response['message'], $response['data']); + return $this->sendResponse($response['data'], $response['message']); + + } + + public function add_member(Request $request, $id) + { + $response = $this->teamRepository->addMember($request, $id); + if (!$response['success']) + return $this->sendError($response['message'], $response['data']); + return $this->sendResponse($response['data'], $response['message']); + + } + + public function remove_member(Request $request, $id) + { + $response = $this->teamRepository->removeMember($request, $id); + if (!$response['success']) + return $this->sendError($response['message'], $response['data']); + return $this->sendResponse($response['data'], $response['message']); + + } } diff --git a/app/Http/Repositories/TeamRepository.php b/app/Http/Repositories/TeamRepository.php index 63c1805..7b533ae 100644 --- a/app/Http/Repositories/TeamRepository.php +++ b/app/Http/Repositories/TeamRepository.php @@ -57,4 +57,38 @@ public function updateTeam($request, $id) $response['message'] = 'Successfully updated the team!'; return $response; } + + public function deleteTeam($id) + { + $response = []; + $team = Team::find($id); + $team->delete(); + $response['success'] = true; + $response['data'] = $team; + $response['message'] = 'Successfully deleted the team!'; + return $response; + } + + public function addMember($request, $id) + { + $response = []; + $team = Team::find($id); + $team->members()->attach($request->user_id); + $response['success'] = true; + $response['data'] = $team; + $response['message'] = 'Successfully added the member!'; + return $response; + } + + + public function removeMember($request, $id) + { + $response = []; + $team = Team::find($id); + $team->members()->detach($request->user_id); + $response['success'] = true; + $response['data'] = $team; + $response['message'] = 'Successfully removed the member!'; + return $response; + } } \ No newline at end of file diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php index dcf70cf..61db09b 100644 --- a/database/seeders/UserSeeder.php +++ b/database/seeders/UserSeeder.php @@ -20,7 +20,7 @@ class UserSeeder extends Seeder */ public function run() { - $track = Track::where('type', 'Flutter Forward Challenges')->first()->pluck('id'); + $track = Track::where('type', 'DEVFEST')->first()->pluck('id'); for($i=1; $i<=15; $i++) { User::create([ 'full_name' => 'username'.$i, diff --git a/routes/api.php b/routes/api.php index c906137..2343554 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,7 +31,7 @@ Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum'); Route::get('/track/{type}/leaderboard', [ParticipantController::class, 'leaderboard'])->middleware(['auth:sanctum', 'verified']); - Route::prefix('admin')->middleware(['auth:sanctum', 'hasRole:admin']) + Route::prefix('admin') ->controller(AdminController::class)->group(function() { Route::get('/stats', 'get_stats'); Route::prefix('user')->group(function() { @@ -65,6 +65,11 @@ Route::get('/', 'get_teams'); Route::post('/create', 'create_team'); Route::post('/{id}/update', 'update_team')->middleware('teamExists'); + Route::delete('/{id}/delete', 'delete_team')->middleware('teamExists'); + Route::post('/{id}/add-member', 'add_member')->middleware('teamExists'); + Route::post('/{id}/remove-member', 'remove_member')->middleware('teamExists'); + + }); }); From 38e619017d4f0234afb397a9796194cb7691985c Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Tue, 5 Dec 2023 16:32:55 +0100 Subject: [PATCH 03/11] test routes --- app/Http/Controllers/API/AdminController.php | 4 +-- app/Http/Kernel.php | 1 + app/Http/Repositories/TeamRepository.php | 32 +++++++++++++------ app/Http/Resources/TeamResource.php | 5 +-- app/Models/User.php | 2 +- .../2014_10_12_000000_create_users_table.php | 1 + database/seeders/AdminSeeder.php | 3 ++ database/seeders/DatabaseSeeder.php | 4 ++- routes/api.php | 2 +- 9 files changed, 37 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/API/AdminController.php b/app/Http/Controllers/API/AdminController.php index 541cac8..279b94a 100644 --- a/app/Http/Controllers/API/AdminController.php +++ b/app/Http/Controllers/API/AdminController.php @@ -185,9 +185,9 @@ public function add_member(Request $request, $id) } - public function remove_member(Request $request, $id) + public function remove_member(Request $request) { - $response = $this->teamRepository->removeMember($request, $id); + $response = $this->teamRepository->removeMember($request); if (!$response['success']) return $this->sendError($response['message'], $response['data']); return $this->sendResponse($response['data'], $response['message']); diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index b2672dd..ed4bad7 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -77,5 +77,6 @@ class Kernel extends HttpKernel 'onlySPA' =>\App\Http\Middleware\OnlySPA::class, 'verifyAuthStep' =>\App\Http\Middleware\EnsureReachedRightStep::class, 'hasAccessToTrack' => \App\Http\Middleware\EnsureHasAccessToTrack::class, + 'teamExists' => \App\Http\Middleware\EnsureTeamExists::class ]; } diff --git a/app/Http/Repositories/TeamRepository.php b/app/Http/Repositories/TeamRepository.php index 7b533ae..0f9c1ee 100644 --- a/app/Http/Repositories/TeamRepository.php +++ b/app/Http/Repositories/TeamRepository.php @@ -2,8 +2,10 @@ namespace App\Http\Repositories; -use App\Http\Resources\Team\TeamResource; +use App\Http\Resources\TeamResource; use App\Models\Team; +use App\Models\User; + use Illuminate\Support\Facades\Validator; class TeamRepository @@ -31,7 +33,10 @@ public function getTeam($id) public function createTeam($request) { $response = []; - $validator = Validator::make($request->all(), ['name' => 'required|unique:teams,name']); + $validator = Validator::make($request->all(), + ['name' => 'required|unique:teams,name', + 'token' => 'required|unique|min:8:teams,token', + ]); if ($validator->fails()) { $response['success'] = false; @@ -39,7 +44,10 @@ public function createTeam($request) $response['data'] = $validator->errors(); return $response; } - $team = Team::create($request); + $team = Team::create([ + 'name' => $request->name, + 'token'=> $request->token, + ]); $response['success'] = true; $response['data'] = $team; $response['message'] = 'Successfully created the team!'; @@ -72,22 +80,26 @@ public function deleteTeam($id) public function addMember($request, $id) { $response = []; - $team = Team::find($id); - $team->members()->attach($request->user_id); + $member = User::find($request->id); + + $member->team_id = $id; + $member->save(); $response['success'] = true; - $response['data'] = $team; + $response['data'] = $member; $response['message'] = 'Successfully added the member!'; return $response; } - public function removeMember($request, $id) + public function removeMember($request) { $response = []; - $team = Team::find($id); - $team->members()->detach($request->user_id); + + $member = User::find($request->id); + $member->team_id = null; + $member->save(); $response['success'] = true; - $response['data'] = $team; + $response['data'] = $member; $response['message'] = 'Successfully removed the member!'; return $response; } diff --git a/app/Http/Resources/TeamResource.php b/app/Http/Resources/TeamResource.php index ced8883..71deaf7 100644 --- a/app/Http/Resources/TeamResource.php +++ b/app/Http/Resources/TeamResource.php @@ -1,6 +1,6 @@ $this->id, 'name' => $this->name, - 'participants' => ParticipantResource::collection(User::where('team_id', $this->id)->get()->toArray()), + 'token'=> $this->token + // 'participants' => ParticipantResource::collection(User::where('team_id', $this->id)->get()->toArray()), ]; } diff --git a/app/Models/User.php b/app/Models/User.php index cc88e55..94e0aed 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -81,7 +81,7 @@ public function username() { public function team() { - return $this->belongsTo('App\Models\Team'); + return $this->belongsTo('App\Models\Team' , 'team_id'); } diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 52aea93..0e31fba 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -16,6 +16,7 @@ public function up() Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->foreignId('track_id')->nullable()->constrained()->onDelete('cascade'); + $table->foreignId('team_id')->nullable(); $table->string('full_name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); diff --git a/database/seeders/AdminSeeder.php b/database/seeders/AdminSeeder.php index 2456da6..9b17c3a 100644 --- a/database/seeders/AdminSeeder.php +++ b/database/seeders/AdminSeeder.php @@ -20,18 +20,21 @@ public function run() 'full_name' => 'abdessamed', 'password' => Hash::make('5RO8TTzmUPg4'), 'role' => 'admin', + "email" => "aa@gmail.com", 'ip' => '127.0.0.1' ]); User::create([ 'full_name' => 'ouael', 'password' => Hash::make('FVFHAOEr3FA8'), 'role' => 'admin', + "email" => "cc@gmail.com", 'ip' => '127.0.0.1' ]); User::create([ 'full_name' => 'abderraouf', 'password' => Hash::make('Ui3tCLLtlpl2'), 'role' => 'admin', + "email" => "bb@gmail.com", 'ip' => '127.0.0.1' ]); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 31a9bb6..0f586d5 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -15,11 +15,13 @@ class DatabaseSeeder extends Seeder public function run() { $this->call([ - TrackSeeder::class, AdminSeeder::class, UserSeeder::class, ChallengeSeeder::class, SubmissionSeeder::class, + TeamSeeder::class, + TrackSeeder::class, + ]); } } diff --git a/routes/api.php b/routes/api.php index 2343554..bd83440 100644 --- a/routes/api.php +++ b/routes/api.php @@ -67,7 +67,7 @@ Route::post('/{id}/update', 'update_team')->middleware('teamExists'); Route::delete('/{id}/delete', 'delete_team')->middleware('teamExists'); Route::post('/{id}/add-member', 'add_member')->middleware('teamExists'); - Route::post('/{id}/remove-member', 'remove_member')->middleware('teamExists'); + Route::post('/remove-member', 'remove_member'); From 22eb777590dafdef8414785f5407ec91958c006c Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Tue, 5 Dec 2023 16:33:27 +0100 Subject: [PATCH 04/11] add team migration --- .../2023_12_05_151101_create_teams_table.php | 33 +++++++++++++++++++ database/seeders/TeamSeeder.php | 28 ++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 database/migrations/2023_12_05_151101_create_teams_table.php create mode 100644 database/seeders/TeamSeeder.php diff --git a/database/migrations/2023_12_05_151101_create_teams_table.php b/database/migrations/2023_12_05_151101_create_teams_table.php new file mode 100644 index 0000000..d7a01f3 --- /dev/null +++ b/database/migrations/2023_12_05_151101_create_teams_table.php @@ -0,0 +1,33 @@ +bigIncrements('id'); + $table->string('name')->unique(); + $table->string('token')->unique(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('teams'); + } +}; diff --git a/database/seeders/TeamSeeder.php b/database/seeders/TeamSeeder.php new file mode 100644 index 0000000..a20a93c --- /dev/null +++ b/database/seeders/TeamSeeder.php @@ -0,0 +1,28 @@ + 'fivex', + 'token' => '5RO8TTzmUPg4', + ]); + Team::create([ + 'name' => 'asdqwe', + 'token' => 'FVFHAOEr3FA8', + ]); + } + +} From e86784b57dc30ec6f8cfba739f391e429c5bfa6a Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Tue, 5 Dec 2023 18:45:16 +0100 Subject: [PATCH 05/11] fix midlleware test --- routes/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/api.php b/routes/api.php index bd83440..340938e 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,7 +31,7 @@ Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum'); Route::get('/track/{type}/leaderboard', [ParticipantController::class, 'leaderboard'])->middleware(['auth:sanctum', 'verified']); - Route::prefix('admin') + Route::prefix('admin')->middleware(['auth:sanctum', 'hasRole:admin', 'verified']) ->controller(AdminController::class)->group(function() { Route::get('/stats', 'get_stats'); Route::prefix('user')->group(function() { From dfc7fd1928db4dffba049fa9daceae36f672a570 Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Wed, 6 Dec 2023 20:10:40 +0100 Subject: [PATCH 06/11] add unit test --- app/Http/Controllers/API/AdminController.php | 6 +- app/Http/Middleware/EnsureTeamExists.php | 6 +- app/Http/Repositories/TeamRepository.php | 73 ++++++++++-- app/Http/Resources/TeamResource.php | 4 +- app/Models/Team.php | 1 - app/Models/User.php | 2 +- database/factories/TeamFactory.php | 25 ++++ ... 2014_10_12_000000_create_teams_table.php} | 0 ... 2014_10_12_000010_create_users_table.php} | 2 +- database/seeders/DatabaseSeeder.php | 6 +- database/seeders/UserSeeder.php | 1 + routes/api.php | 3 +- .../Feature/Admin/Teams/AddTeamMemberTest.php | 112 ++++++++++++++++++ tests/Feature/Admin/Teams/CreateTeamTest.php | 93 +++++++++++++++ tests/Feature/Admin/Teams/DeleteTeamTest.php | 57 +++++++++ tests/Feature/Admin/Teams/GetAllTeamsTest.php | 41 +++++++ tests/Feature/Admin/Teams/GetTeamByIdTest.php | 55 +++++++++ .../Admin/Teams/RemoveTeamMemberTest.php | 111 +++++++++++++++++ tests/Feature/Admin/Teams/UpdateTeamTest.php | 65 ++++++++++ 19 files changed, 637 insertions(+), 26 deletions(-) create mode 100644 database/factories/TeamFactory.php rename database/migrations/{2023_12_05_151101_create_teams_table.php => 2014_10_12_000000_create_teams_table.php} (100%) rename database/migrations/{2014_10_12_000000_create_users_table.php => 2014_10_12_000010_create_users_table.php} (92%) create mode 100644 tests/Feature/Admin/Teams/AddTeamMemberTest.php create mode 100644 tests/Feature/Admin/Teams/CreateTeamTest.php create mode 100644 tests/Feature/Admin/Teams/DeleteTeamTest.php create mode 100644 tests/Feature/Admin/Teams/GetAllTeamsTest.php create mode 100644 tests/Feature/Admin/Teams/GetTeamByIdTest.php create mode 100644 tests/Feature/Admin/Teams/RemoveTeamMemberTest.php create mode 100644 tests/Feature/Admin/Teams/UpdateTeamTest.php diff --git a/app/Http/Controllers/API/AdminController.php b/app/Http/Controllers/API/AdminController.php index 279b94a..9285d77 100644 --- a/app/Http/Controllers/API/AdminController.php +++ b/app/Http/Controllers/API/AdminController.php @@ -170,8 +170,6 @@ public function update_team(Request $request, $id) public function delete_team($id) { $response = $this->teamRepository->deleteTeam($id); - if (!$response['success']) - return $this->sendError($response['message'], $response['data']); return $this->sendResponse($response['data'], $response['message']); } @@ -180,7 +178,7 @@ public function add_member(Request $request, $id) { $response = $this->teamRepository->addMember($request, $id); if (!$response['success']) - return $this->sendError($response['message'], $response['data']); + return $this->sendError($response['message'], $response['data']); return $this->sendResponse($response['data'], $response['message']); } @@ -189,7 +187,7 @@ public function remove_member(Request $request) { $response = $this->teamRepository->removeMember($request); if (!$response['success']) - return $this->sendError($response['message'], $response['data']); + return $this->sendError($response['message'], $response['data']); return $this->sendResponse($response['data'], $response['message']); } diff --git a/app/Http/Middleware/EnsureTeamExists.php b/app/Http/Middleware/EnsureTeamExists.php index 57a600d..19e7dc3 100644 --- a/app/Http/Middleware/EnsureTeamExists.php +++ b/app/Http/Middleware/EnsureTeamExists.php @@ -17,11 +17,11 @@ class EnsureTeamExists */ public function handle(Request $request, Closure $next) { - $challenge = Team::find($request->route('id')); - if (!$challenge) + $team = Team::find($request->route('id')); + if (!$team) return response()->json([ 'success' => false, - 'message' => 'Team can not be found' + 'message' => 'Team can not be found!' ], 400); return $next($request); diff --git a/app/Http/Repositories/TeamRepository.php b/app/Http/Repositories/TeamRepository.php index 0f9c1ee..23f8c4c 100644 --- a/app/Http/Repositories/TeamRepository.php +++ b/app/Http/Repositories/TeamRepository.php @@ -3,6 +3,7 @@ namespace App\Http\Repositories; use App\Http\Resources\TeamResource; +use App\Http\Resources\User\ParticipantResource; use App\Models\Team; use App\Models\User; @@ -16,7 +17,7 @@ public function getTeams() $teams = Team::all(); $response['success'] = true; $response['data'] = TeamResource::collection($teams); - $response['message'] = 'Successfully retrieved all the users!'; + $response['message'] = 'Successfully retrieved all the teams!'; return $response; } @@ -35,9 +36,8 @@ public function createTeam($request) $response = []; $validator = Validator::make($request->all(), ['name' => 'required|unique:teams,name', - 'token' => 'required|unique|min:8:teams,token', + 'token' => 'required|unique:teams,token|min:8', ]); - if ($validator->fails()) { $response['success'] = false; $response['message'] = 'Validation failed!'; @@ -49,19 +49,32 @@ public function createTeam($request) 'token'=> $request->token, ]); $response['success'] = true; - $response['data'] = $team; + $response['data'] = new TeamResource($team); $response['message'] = 'Successfully created the team!'; return $response; } public function updateTeam($request, $id) { + $response = []; + + $validator = Validator::make($request->all(), + ['name' => 'required|unique:teams,name', + ]); + + if ($validator->fails()) { + $response['success'] = false; + $response['message'] = 'Validation failed!'; + $response['data'] = $validator->errors(); + return $response; + } + $team = Team::find($id); $team->name = $request->name; $team->save(); $response['success'] = true; - $response['data'] = $team; + $response['data'] = new TeamResource($team); $response['message'] = 'Successfully updated the team!'; return $response; } @@ -72,7 +85,7 @@ public function deleteTeam($id) $team = Team::find($id); $team->delete(); $response['success'] = true; - $response['data'] = $team; + $response['data'] = []; $response['message'] = 'Successfully deleted the team!'; return $response; } @@ -80,12 +93,32 @@ public function deleteTeam($id) public function addMember($request, $id) { $response = []; - $member = User::find($request->id); + + $validator = Validator::make($request->all(), + ['participant_id' => 'required', + ]); + + if ($validator->fails()) { + $response['success'] = false; + $response['message'] = 'Validation failed!'; + $response['data'] = $validator->errors(); + return $response; + } + + + $member = User::find($request->participant_id); + if (!$member) { + $response['success'] = false; + $response['message'] = 'Participant not found!'; + $response['data'] = []; + return $response; + } + $member->team_id = $id; $member->save(); $response['success'] = true; - $response['data'] = $member; + $response['data'] = new ParticipantResource($member); $response['message'] = 'Successfully added the member!'; return $response; } @@ -95,11 +128,31 @@ public function removeMember($request) { $response = []; - $member = User::find($request->id); + $validator = Validator::make($request->all(), + ['participant_id' => 'required', + ]); + + if ($validator->fails()) { + $response['success'] = false; + $response['message'] = 'Validation failed!'; + $response['data'] = $validator->errors(); + return $response; + } + + + + $member = User::find($request->participant_id); + if (!$member) { + $response['success'] = false; + $response['message'] = 'Participant not found!'; + $response['data'] = []; + return $response; + } + $member->team_id = null; $member->save(); $response['success'] = true; - $response['data'] = $member; + $response['data'] = new ParticipantResource($member); $response['message'] = 'Successfully removed the member!'; return $response; } diff --git a/app/Http/Resources/TeamResource.php b/app/Http/Resources/TeamResource.php index 71deaf7..239b81b 100644 --- a/app/Http/Resources/TeamResource.php +++ b/app/Http/Resources/TeamResource.php @@ -15,8 +15,8 @@ public function toArray($request) return [ 'id' => $this->id, 'name' => $this->name, - 'token'=> $this->token - // 'participants' => ParticipantResource::collection(User::where('team_id', $this->id)->get()->toArray()), + 'token'=> $this->token, + 'participants' => ParticipantResource::collection($this->participants), ]; } diff --git a/app/Models/Team.php b/app/Models/Team.php index e4b50a4..cc5452a 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -10,7 +10,6 @@ class Team extends Model use HasFactory; protected $fillable = ['name', 'token']; - //Relationship public function participants() { diff --git a/app/Models/User.php b/app/Models/User.php index 94e0aed..cc88e55 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -81,7 +81,7 @@ public function username() { public function team() { - return $this->belongsTo('App\Models\Team' , 'team_id'); + return $this->belongsTo('App\Models\Team'); } diff --git a/database/factories/TeamFactory.php b/database/factories/TeamFactory.php new file mode 100644 index 0000000..3dc7b1e --- /dev/null +++ b/database/factories/TeamFactory.php @@ -0,0 +1,25 @@ + + */ +class TeamFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'name' => fake()->name(), + 'token' => fake()->text(14), + ]; + } +} diff --git a/database/migrations/2023_12_05_151101_create_teams_table.php b/database/migrations/2014_10_12_000000_create_teams_table.php similarity index 100% rename from database/migrations/2023_12_05_151101_create_teams_table.php rename to database/migrations/2014_10_12_000000_create_teams_table.php diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000010_create_users_table.php similarity index 92% rename from database/migrations/2014_10_12_000000_create_users_table.php rename to database/migrations/2014_10_12_000010_create_users_table.php index 0e31fba..a62b106 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000010_create_users_table.php @@ -16,7 +16,7 @@ public function up() Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->foreignId('track_id')->nullable()->constrained()->onDelete('cascade'); - $table->foreignId('team_id')->nullable(); + $table->foreignId('team_id')->nullable()->constrained()->nullOnDelete(); $table->string('full_name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0f586d5..d84f9d7 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -15,12 +15,12 @@ class DatabaseSeeder extends Seeder public function run() { $this->call([ - AdminSeeder::class, + TrackSeeder::class, + TeamSeeder::class, UserSeeder::class, + AdminSeeder::class, ChallengeSeeder::class, SubmissionSeeder::class, - TeamSeeder::class, - TrackSeeder::class, ]); } diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php index 61db09b..aadfa2f 100644 --- a/database/seeders/UserSeeder.php +++ b/database/seeders/UserSeeder.php @@ -28,6 +28,7 @@ public function run() 'points' => 0, 'role' => 'participant', 'ip' => '127.0.0.1', + 'team_id' => "1", 'track_id' => $track ]); } diff --git a/routes/api.php b/routes/api.php index 340938e..3af41ca 100644 --- a/routes/api.php +++ b/routes/api.php @@ -31,7 +31,7 @@ Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum'); Route::get('/track/{type}/leaderboard', [ParticipantController::class, 'leaderboard'])->middleware(['auth:sanctum', 'verified']); - Route::prefix('admin')->middleware(['auth:sanctum', 'hasRole:admin', 'verified']) + Route::prefix('admin')->middleware(['auth:sanctum', 'hasRole:admin']) ->controller(AdminController::class)->group(function() { Route::get('/stats', 'get_stats'); Route::prefix('user')->group(function() { @@ -63,6 +63,7 @@ Route::prefix('team')->group(function () { Route::get('/', 'get_teams'); + Route::get('/{id}', 'get_team')->middleware('teamExists'); Route::post('/create', 'create_team'); Route::post('/{id}/update', 'update_team')->middleware('teamExists'); Route::delete('/{id}/delete', 'delete_team')->middleware('teamExists'); diff --git a/tests/Feature/Admin/Teams/AddTeamMemberTest.php b/tests/Feature/Admin/Teams/AddTeamMemberTest.php new file mode 100644 index 0000000..2a4f395 --- /dev/null +++ b/tests/Feature/Admin/Teams/AddTeamMemberTest.php @@ -0,0 +1,112 @@ +create(); + $track = Track::factory()->create(); + + $participant = User::factory()->create([ + 'track_id' => $track->id, + ]); + + + $payload = [ + 'participant_id' => $participant->id, + ]; + + + + $response = $this->postJson($this->endpoint.$team->id.'/add-member', $payload); + + $response->assertStatus(Response::HTTP_OK)->assertExactJson([ + 'success' => true, + 'data' => [ + 'id' => $participant->id, + 'full_name' => $participant->full_name, + 'email' => $participant->email, + 'points' => 0, + 'role' => 'participant', + 'email_verified' => $participant->email_verified_at ? true: false, + 'track' => $track->type, + 'submissions' => [], + 'team' => $team->name, + ], + 'message' => 'Successfully added the member!' + ]); + + $this->assertDatabaseCount('teams', 1); + + $this->assertDatabaseHas('teams', [ + 'id' => $team->id, + 'name' => $team->name, + ]); + + $this->assertDatabaseCount('users', 2); + + $this->assertDatabaseHas('users', [ + 'team_id' => $team->id, + ]); + + + + } + + + public function test_add_team_member_without_data() + { + $team = Team::factory()->create(); + $payload = []; + + $response = $this->postJson($this->endpoint.$team->id.'/add-member', $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Validation failed!', + 'data' => [ + 'participant_id' => ['The participant id field is required.'], + ], + ]); + } + + + public function test_add_team_member_does_not_exist() + { + $team = Team::factory()->create(); + $payload = [ + 'participant_id' => 100, + ]; + + $response = $this->postJson($this->endpoint.$team->id.'/add-member', $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Participant not found!', + ]); + + $this->assertDatabaseCount('teams', 1); + + + } + + +} diff --git a/tests/Feature/Admin/Teams/CreateTeamTest.php b/tests/Feature/Admin/Teams/CreateTeamTest.php new file mode 100644 index 0000000..d9b20dd --- /dev/null +++ b/tests/Feature/Admin/Teams/CreateTeamTest.php @@ -0,0 +1,93 @@ + $this->faker->text(8), + 'token' => $this->faker->text(30) + ]; + + $response = $this->postJson($this->endpoint, $payload); + + $response->assertStatus(Response::HTTP_OK)->assertExactJson([ + 'success' => true, + 'data' => [ + 'name' => $payload['name'], + 'token' => $payload['token'], + 'participants' => [], + 'id' => 1, + ], + 'message' => 'Successfully created the team!' + ]); + + $this->assertDatabaseCount('teams', 1); + $this->assertDatabaseHas('teams', [ + 'name' => $payload['name'], + 'token' => $payload['token'], + ]); + } + + /** + * A feature test for creating a team without data. + * + * @return void + */ + public function test_creating_team_without_data() + { + $payload = []; + + $response = $this->postJson($this->endpoint, $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Validation failed!', + 'data' => [ + 'name' => ['The name field is required.'], + 'token' => ['The token field is required.'], + ], + ]); + + $this->assertDatabaseCount('teams', 0); + } + + /** + * A feature test for creating a team that exists. + * + * @return void + */ + public function test_create_team_that_exists() + { + $team = Team::factory()->create(); + + $payload = [ + 'name' => $team->name, + 'token' => $this->faker->text(30) + ]; + + $response = $this->postJson($this->endpoint, $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Validation failed!', + 'data' => [ + 'name' => ['The name has already been taken.'], + ], + ]); + + $this->assertDatabaseCount('teams', 1); + } +} diff --git a/tests/Feature/Admin/Teams/DeleteTeamTest.php b/tests/Feature/Admin/Teams/DeleteTeamTest.php new file mode 100644 index 0000000..ddd8435 --- /dev/null +++ b/tests/Feature/Admin/Teams/DeleteTeamTest.php @@ -0,0 +1,57 @@ +create(); + + $response = $this->deleteJson($this->endpoint.$team->id.'/delete'); + + $response->assertStatus(Response::HTTP_OK)->assertExactJson([ + 'success' => true, + 'data' => [], + 'message' => 'Successfully deleted the team!', + ]); + + $this->assertDatabaseCount('teams',0); + + $this->assertDatabaseMissing('teams', [ + 'name' => $team->name, + ]); + + + + } + + /** + * A feature test for unsuccessfull deletion because of unexisting team. + * + * @return void + */ + + public function test_delete_unexisting_team() + { + $response = $this->deleteJson($this->endpoint.'2333'.'/delete'); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Team can not be found!' + ]); + + $this->assertDatabaseCount('teams', 0); + } + +} diff --git a/tests/Feature/Admin/Teams/GetAllTeamsTest.php b/tests/Feature/Admin/Teams/GetAllTeamsTest.php new file mode 100644 index 0000000..f84bdd8 --- /dev/null +++ b/tests/Feature/Admin/Teams/GetAllTeamsTest.php @@ -0,0 +1,41 @@ +count(3)->create(); + + $response = $this->getJson($this->endpoint); + $response + ->assertStatus(Response::HTTP_OK) + ->assertJsonStructure([ + 'success', + 'data' => [ + '*' => [ + 'id', + 'name', + 'token', + ] + ], + 'message' + ]) + ; + $this->assertTrue($response["success"], true); + $this->assertEquals($response["message"], "Successfully retrieved all the teams!"); + $this->assertDatabaseCount('teams', 3); + } +} diff --git a/tests/Feature/Admin/Teams/GetTeamByIdTest.php b/tests/Feature/Admin/Teams/GetTeamByIdTest.php new file mode 100644 index 0000000..88ccb53 --- /dev/null +++ b/tests/Feature/Admin/Teams/GetTeamByIdTest.php @@ -0,0 +1,55 @@ +create(); + + $response = $this->getJson($this->endpoint.$teams->id); + $response + ->assertStatus(Response::HTTP_OK) + ->assertExactJson([ + 'success' => true, + 'data' => [ + "id" => $teams->id, + "name" => $teams->name, + "token" => $teams->token, + "participants" => $teams->participants, + ], + 'message' => "Successfully retrieved the team!", + ]); + + $this->assertDatabaseCount('teams', 1); + + + } + + + public function test_get_unexisting_team() + { + $response = $this->getJson($this->endpoint.'2333'); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Team can not be found!' + ]); + + $this->assertDatabaseCount('teams', 0); + } + + +} diff --git a/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php b/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php new file mode 100644 index 0000000..fd4eed9 --- /dev/null +++ b/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php @@ -0,0 +1,111 @@ +create(); + $track = Track::factory()->create(); + + $participant = User::factory()->create([ + 'track_id' => $track->id, + 'team_id' => $team->id, + ]); + + + $payload = [ + 'participant_id' => $participant->id, + ]; + + + + $response = $this->postJson($this->endpoint, $payload); + + $response->assertStatus(Response::HTTP_OK)->assertExactJson([ + 'success' => true, + 'data' => [ + 'id' => $participant->id, + 'full_name' => $participant->full_name, + 'email' => $participant->email, + 'points' => 0, + 'role' => 'participant', + 'email_verified' => $participant->email_verified_at ? true: false, + 'track' => $track->type, + 'submissions' => [], + 'team' => null, + ], + 'message' => 'Successfully removed the member!' + ]); + + $this->assertDatabaseCount('teams', 1); + + $this->assertDatabaseHas('teams', [ + 'id' => $team->id, + 'name' => $team->name, + ]); + + $this->assertDatabaseCount('users', 2); + + $this->assertDatabaseHas('users', [ + 'team_id' => null, + ]); + + + + } + + + public function test_remove_team_member_without_data() + { + $payload = []; + + $response = $this->postJson($this->endpoint, $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Validation failed!', + 'data' => [ + 'participant_id' => ['The participant id field is required.'], + ], + ]); + + $this->assertDatabaseCount('teams', 0); + } + + public function test_remove_team_member_does_not_exist() + { + $payload = [ + 'participant_id' => 878787, + ]; + + $response = $this->postJson($this->endpoint, $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Participant not found!', + ]); + + $this->assertDatabaseCount('teams', 0); + } + + + +} diff --git a/tests/Feature/Admin/Teams/UpdateTeamTest.php b/tests/Feature/Admin/Teams/UpdateTeamTest.php new file mode 100644 index 0000000..ced33a5 --- /dev/null +++ b/tests/Feature/Admin/Teams/UpdateTeamTest.php @@ -0,0 +1,65 @@ +create(); + $payload = [ + 'name' => $this->faker->text(30) + ]; + + $response = $this->postJson($this->endpoint.$team->id.'/update', $payload); + + $response->assertStatus(Response::HTTP_OK)->assertExactJson([ + 'success' => true, + 'data' => [ + 'id' => $team->id, + 'name' => $payload['name'], + 'token' => $team->token, + 'participants' => $team->participants, + ], + 'message' => 'Successfully updated the team!' + ]); + + $this->assertDatabaseMissing('teams', [ + 'token' => $team->token, + 'name' => $team->name + ]); + $this->assertDatabaseHas('teams', [ + 'token' => $team->token, + 'name' => $payload['name'] + ]); + } + + public function test_update_team_name_without_data() + { + $team = Team::factory()->create(); + $payload = []; + + $response = $this->postJson($this->endpoint.$team->id.'/update', $payload); + + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Validation failed!', + 'data' => [ + 'name' => ['The name field is required.'], + ], + ]); + } + + +} From bb4e184a9bf0c5cf04d84f2c2e66038856a29a63 Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Wed, 6 Dec 2023 21:12:14 +0100 Subject: [PATCH 07/11] update admin controller --- app/Http/Controllers/API/AdminController.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/API/AdminController.php b/app/Http/Controllers/API/AdminController.php index 9285d77..b87ab8c 100644 --- a/app/Http/Controllers/API/AdminController.php +++ b/app/Http/Controllers/API/AdminController.php @@ -177,8 +177,8 @@ public function delete_team($id) public function add_member(Request $request, $id) { $response = $this->teamRepository->addMember($request, $id); - if (!$response['success']) - return $this->sendError($response['message'], $response['data']); + if (!$response['success']) return $this->sendError($response['message'], $response['data']); + return $this->sendResponse($response['data'], $response['message']); } @@ -186,8 +186,7 @@ public function add_member(Request $request, $id) public function remove_member(Request $request) { $response = $this->teamRepository->removeMember($request); - if (!$response['success']) - return $this->sendError($response['message'], $response['data']); + if (!$response['success']) return $this->sendError($response['message'], $response['data']); return $this->sendResponse($response['data'], $response['message']); } From b9fb75e9dfefdef6a890e19c93f4d0614fc913a7 Mon Sep 17 00:00:00 2001 From: MoussaabBadla Date: Fri, 8 Dec 2023 15:00:32 +0100 Subject: [PATCH 08/11] add missing tests --- .../Feature/Admin/Teams/AddTeamMemberTest.php | 37 ++----------------- tests/Feature/Admin/Teams/CreateTeamTest.php | 12 ------ tests/Feature/Admin/Teams/DeleteTeamTest.php | 10 ----- tests/Feature/Admin/Teams/GetAllTeamsTest.php | 1 - tests/Feature/Admin/Teams/GetTeamByIdTest.php | 11 ------ .../Admin/Teams/RemoveTeamMemberTest.php | 29 ++------------- tests/Feature/Admin/Teams/UpdateTeamTest.php | 16 ++++++-- 7 files changed, 18 insertions(+), 98 deletions(-) diff --git a/tests/Feature/Admin/Teams/AddTeamMemberTest.php b/tests/Feature/Admin/Teams/AddTeamMemberTest.php index 2a4f395..ed06edf 100644 --- a/tests/Feature/Admin/Teams/AddTeamMemberTest.php +++ b/tests/Feature/Admin/Teams/AddTeamMemberTest.php @@ -20,24 +20,15 @@ class AddTeamMemberTest extends AdminTestCase */ public function test_add_team_member() { - - $team = Team::factory()->create(); $track = Track::factory()->create(); - $participant = User::factory()->create([ 'track_id' => $track->id, ]); - - $payload = [ 'participant_id' => $participant->id, ]; - - - $response = $this->postJson($this->endpoint.$team->id.'/add-member', $payload); - $response->assertStatus(Response::HTTP_OK)->assertExactJson([ 'success' => true, 'data' => [ @@ -53,32 +44,20 @@ public function test_add_team_member() ], 'message' => 'Successfully added the member!' ]); - - $this->assertDatabaseCount('teams', 1); - + $this->assertDatabaseHas('users', [ + 'team_id' => $team->id, + ]); $this->assertDatabaseHas('teams', [ 'id' => $team->id, 'name' => $team->name, ]); - $this->assertDatabaseCount('users', 2); - - $this->assertDatabaseHas('users', [ - 'team_id' => $team->id, - ]); - - - } - - public function test_add_team_member_without_data() { $team = Team::factory()->create(); $payload = []; - $response = $this->postJson($this->endpoint.$team->id.'/add-member', $payload); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Validation failed!', @@ -87,26 +66,16 @@ public function test_add_team_member_without_data() ], ]); } - - public function test_add_team_member_does_not_exist() { $team = Team::factory()->create(); $payload = [ 'participant_id' => 100, ]; - $response = $this->postJson($this->endpoint.$team->id.'/add-member', $payload); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Participant not found!', ]); - - $this->assertDatabaseCount('teams', 1); - - } - - } diff --git a/tests/Feature/Admin/Teams/CreateTeamTest.php b/tests/Feature/Admin/Teams/CreateTeamTest.php index d9b20dd..4605609 100644 --- a/tests/Feature/Admin/Teams/CreateTeamTest.php +++ b/tests/Feature/Admin/Teams/CreateTeamTest.php @@ -20,9 +20,7 @@ public function test_create_team() 'name' => $this->faker->text(8), 'token' => $this->faker->text(30) ]; - $response = $this->postJson($this->endpoint, $payload); - $response->assertStatus(Response::HTTP_OK)->assertExactJson([ 'success' => true, 'data' => [ @@ -33,14 +31,12 @@ public function test_create_team() ], 'message' => 'Successfully created the team!' ]); - $this->assertDatabaseCount('teams', 1); $this->assertDatabaseHas('teams', [ 'name' => $payload['name'], 'token' => $payload['token'], ]); } - /** * A feature test for creating a team without data. * @@ -49,9 +45,7 @@ public function test_create_team() public function test_creating_team_without_data() { $payload = []; - $response = $this->postJson($this->endpoint, $payload); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Validation failed!', @@ -60,10 +54,8 @@ public function test_creating_team_without_data() 'token' => ['The token field is required.'], ], ]); - $this->assertDatabaseCount('teams', 0); } - /** * A feature test for creating a team that exists. * @@ -72,14 +64,11 @@ public function test_creating_team_without_data() public function test_create_team_that_exists() { $team = Team::factory()->create(); - $payload = [ 'name' => $team->name, 'token' => $this->faker->text(30) ]; - $response = $this->postJson($this->endpoint, $payload); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Validation failed!', @@ -87,7 +76,6 @@ public function test_create_team_that_exists() 'name' => ['The name has already been taken.'], ], ]); - $this->assertDatabaseCount('teams', 1); } } diff --git a/tests/Feature/Admin/Teams/DeleteTeamTest.php b/tests/Feature/Admin/Teams/DeleteTeamTest.php index ddd8435..872f56f 100644 --- a/tests/Feature/Admin/Teams/DeleteTeamTest.php +++ b/tests/Feature/Admin/Teams/DeleteTeamTest.php @@ -17,40 +17,30 @@ class DeleteTeamTest extends AdminTestCase public function test_delete_team() { $team = Team::factory()->create(); - $response = $this->deleteJson($this->endpoint.$team->id.'/delete'); - $response->assertStatus(Response::HTTP_OK)->assertExactJson([ 'success' => true, 'data' => [], 'message' => 'Successfully deleted the team!', ]); - $this->assertDatabaseCount('teams',0); $this->assertDatabaseMissing('teams', [ 'name' => $team->name, ]); - - - } - /** * A feature test for unsuccessfull deletion because of unexisting team. * * @return void */ - public function test_delete_unexisting_team() { $response = $this->deleteJson($this->endpoint.'2333'.'/delete'); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Team can not be found!' ]); - $this->assertDatabaseCount('teams', 0); } diff --git a/tests/Feature/Admin/Teams/GetAllTeamsTest.php b/tests/Feature/Admin/Teams/GetAllTeamsTest.php index f84bdd8..b6a7bb7 100644 --- a/tests/Feature/Admin/Teams/GetAllTeamsTest.php +++ b/tests/Feature/Admin/Teams/GetAllTeamsTest.php @@ -18,7 +18,6 @@ class GetAllTeamsTest extends AdminTestCase public function test_get_all_teams() { $teams = Team::factory()->count(3)->create(); - $response = $this->getJson($this->endpoint); $response ->assertStatus(Response::HTTP_OK) diff --git a/tests/Feature/Admin/Teams/GetTeamByIdTest.php b/tests/Feature/Admin/Teams/GetTeamByIdTest.php index 88ccb53..7545caa 100644 --- a/tests/Feature/Admin/Teams/GetTeamByIdTest.php +++ b/tests/Feature/Admin/Teams/GetTeamByIdTest.php @@ -18,7 +18,6 @@ class GetTeamByIdTest extends AdminTestCase public function test_get_team_by_id() { $teams = Team::factory()->create(); - $response = $this->getJson($this->endpoint.$teams->id); $response ->assertStatus(Response::HTTP_OK) @@ -32,24 +31,14 @@ public function test_get_team_by_id() ], 'message' => "Successfully retrieved the team!", ]); - - $this->assertDatabaseCount('teams', 1); - - } - - public function test_get_unexisting_team() { $response = $this->getJson($this->endpoint.'2333'); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Team can not be found!' ]); - $this->assertDatabaseCount('teams', 0); } - - } diff --git a/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php b/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php index fd4eed9..b220044 100644 --- a/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php +++ b/tests/Feature/Admin/Teams/RemoveTeamMemberTest.php @@ -20,8 +20,6 @@ class RemoveTeamMemberTest extends AdminTestCase */ public function test_remove_team_member() { - - $team = Team::factory()->create(); $track = Track::factory()->create(); @@ -29,16 +27,10 @@ public function test_remove_team_member() 'track_id' => $track->id, 'team_id' => $team->id, ]); - - $payload = [ 'participant_id' => $participant->id, ]; - - - $response = $this->postJson($this->endpoint, $payload); - $response->assertStatus(Response::HTTP_OK)->assertExactJson([ 'success' => true, 'data' => [ @@ -54,31 +46,21 @@ public function test_remove_team_member() ], 'message' => 'Successfully removed the member!' ]); - $this->assertDatabaseCount('teams', 1); - $this->assertDatabaseHas('teams', [ 'id' => $team->id, 'name' => $team->name, ]); - $this->assertDatabaseCount('users', 2); - $this->assertDatabaseHas('users', [ + 'id' => $participant->id, 'team_id' => null, ]); - - - } - - public function test_remove_team_member_without_data() { $payload = []; - $response = $this->postJson($this->endpoint, $payload); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Validation failed!', @@ -87,25 +69,20 @@ public function test_remove_team_member_without_data() ], ]); - $this->assertDatabaseCount('teams', 0); } - public function test_remove_team_member_does_not_exist() { $payload = [ 'participant_id' => 878787, ]; - $response = $this->postJson($this->endpoint, $payload); - $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ 'success' => false, 'message' => 'Participant not found!', ]); - - $this->assertDatabaseCount('teams', 0); } +} + -} diff --git a/tests/Feature/Admin/Teams/UpdateTeamTest.php b/tests/Feature/Admin/Teams/UpdateTeamTest.php index ced33a5..13a644b 100644 --- a/tests/Feature/Admin/Teams/UpdateTeamTest.php +++ b/tests/Feature/Admin/Teams/UpdateTeamTest.php @@ -21,7 +21,6 @@ public function test_update_team_name() $payload = [ 'name' => $this->faker->text(30) ]; - $response = $this->postJson($this->endpoint.$team->id.'/update', $payload); $response->assertStatus(Response::HTTP_OK)->assertExactJson([ @@ -44,7 +43,6 @@ public function test_update_team_name() 'name' => $payload['name'] ]); } - public function test_update_team_name_without_data() { $team = Team::factory()->create(); @@ -60,6 +58,16 @@ public function test_update_team_name_without_data() ], ]); } - - + public function test_update_team_name_dont_exist() + { + $payload = [ + "name" => $this->faker->name() + ]; + $response = $this->postJson($this->endpoint."233".'/update', $payload); + $response->assertStatus(Response::HTTP_BAD_REQUEST)->assertExactJson([ + 'success' => false, + 'message' => 'Team can not be found!', + ]); + $this->assertDatabaseCount('teams', 0); + } } From 05fe436b3e5fd4cdf72fed23f4bdc1a9d73a4111 Mon Sep 17 00:00:00 2001 From: Moussaab Badla <106885435+MoussaabBadla@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:21:30 +0100 Subject: [PATCH 09/11] Update tests/Feature/Admin/Teams/AddTeamMemberTest.php Co-authored-by: Mohamed Abdessamed <75367288+Brivan-26@users.noreply.github.com> --- tests/Feature/Admin/Teams/AddTeamMemberTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Feature/Admin/Teams/AddTeamMemberTest.php b/tests/Feature/Admin/Teams/AddTeamMemberTest.php index ed06edf..906135a 100644 --- a/tests/Feature/Admin/Teams/AddTeamMemberTest.php +++ b/tests/Feature/Admin/Teams/AddTeamMemberTest.php @@ -47,10 +47,6 @@ public function test_add_team_member() $this->assertDatabaseHas('users', [ 'team_id' => $team->id, ]); - $this->assertDatabaseHas('teams', [ - 'id' => $team->id, - 'name' => $team->name, - ]); $this->assertDatabaseCount('users', 2); } public function test_add_team_member_without_data() From eb27acc081f694ac089ea01963d924d87a860f0d Mon Sep 17 00:00:00 2001 From: Moussaab Badla <106885435+MoussaabBadla@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:22:11 +0100 Subject: [PATCH 10/11] Update tests/Feature/Admin/Teams/AddTeamMemberTest.php Co-authored-by: Mohamed Abdessamed <75367288+Brivan-26@users.noreply.github.com> --- tests/Feature/Admin/Teams/AddTeamMemberTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Feature/Admin/Teams/AddTeamMemberTest.php b/tests/Feature/Admin/Teams/AddTeamMemberTest.php index 906135a..c472ac2 100644 --- a/tests/Feature/Admin/Teams/AddTeamMemberTest.php +++ b/tests/Feature/Admin/Teams/AddTeamMemberTest.php @@ -47,7 +47,6 @@ public function test_add_team_member() $this->assertDatabaseHas('users', [ 'team_id' => $team->id, ]); - $this->assertDatabaseCount('users', 2); } public function test_add_team_member_without_data() { From 71be001309ef323875956821e74fc6a436ae7d9b Mon Sep 17 00:00:00 2001 From: Moussaab Badla <106885435+MoussaabBadla@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:22:54 +0100 Subject: [PATCH 11/11] Update tests/Feature/Admin/Teams/GetTeamByIdTest.php Co-authored-by: Mohamed Abdessamed <75367288+Brivan-26@users.noreply.github.com> --- tests/Feature/Admin/Teams/GetTeamByIdTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Feature/Admin/Teams/GetTeamByIdTest.php b/tests/Feature/Admin/Teams/GetTeamByIdTest.php index 7545caa..0fbae2e 100644 --- a/tests/Feature/Admin/Teams/GetTeamByIdTest.php +++ b/tests/Feature/Admin/Teams/GetTeamByIdTest.php @@ -39,6 +39,5 @@ public function test_get_unexisting_team() 'success' => false, 'message' => 'Team can not be found!' ]); - $this->assertDatabaseCount('teams', 0); } }