Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration tests, user importation, daily user course activity auto (cron job) importation #133

Merged
merged 14 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ APP_KEY=base64:tMvGB75Elv+M6zi6PazmUp7qNyEAALwp5BA53sVu5E4=
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306

USERS_INFO="chrisS,[email protected],Chris,Santillan,8,[email protected],1;nokieR,[email protected],Nokie,Rae,10,[email protected],1;prestonT,[email protected],Preston,Thorpe,1,[email protected],1;coleD,[email protected],Cole,Dykstra,7,[email protected],1"
SEED_COURSES='"Entrepreneurship 101","ENT101","Learn to launch and grow innovative business with essential entrepreneurial skills and strategies",5,"https://st3.depositphotos.com/3591429/12631/i/450/depositphotos_126313394-stock-photo-business-of-people-with-handshake.jpg",13,2;"Anger Management","AMGMT101","Develop skills to effectively manage anger: fostering personal growth and stronger relationships",6,"https://www.shutterstock.com/shutterstock/photos/434504767/display_1500/stock-photo-hand-holding-piece-of-jigsaw-puzzle-with-word-anger-management-434504767.jpg",15,2;"Business Analytics","BBA101","Drive strategic decisions by mastering analytical tools that transform data into business insights.",4,"https://t4.ftcdn.net/jpg/01/72/67/07/360_F_172670704_DtUzeuEo8sTFOgNdJgqEtAOKM3YLrgIv.jpg",11,2;"Introduction to Cyber Security","CYSEC101","Master the art of protecting systems and data against complex cyber threat and vulnerability.",3,"https://media.istockphoto.com/id/1331943958/photo/digital-cloud-security-background-concept.jpg?s=612x612&w=0&k=20&c=ktHShoivHgGcXbkgJGUpy6Q5JLYKMGsrbpY0e_4MrSc=",9,2'

CANVAS_BASE_URL=https://canvas.unlockedlabs.xyz
CANVAS_ACCOUNT_ID=1
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
run: php artisan key:generate --env=testing

- name: Run Testsuite
env:
CANVAS_API_KEY: ${{ secrets.CANVAS_API_KEY }}
CI: true
run: php artisan test

deploy:
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->job('UserCourseActivityTask')->dailyAt(time('23:59:00'));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Enums/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

enum UserRole: string
{
case Admin = 'admin';
case Student = 'student';
case ADMIN = 'admin';
case STUDENT = 'student';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function store(Request $request): RedirectResponse
'name_last' => $request->name_last,
'password' => Hash::make($request->password),
'reset_password' => false,
'role' => UserRole::Student,
'role' => UserRole::STUDENT,
]);

event(new Registered($user));
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/StudentNewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function store(Request $request): JsonResponse|RedirectResponse
// set `reset_password` to `true` so that on next login they can be
// prompted to set their new password.
$user = User::where('username', $request->username)->first();
if ($user['role'] === UserRole::Student) {
if ($user['role'] === UserRole::STUDENT) {
$pw = $user->createTempPassword();

return response()->json([
Expand Down
47 changes: 47 additions & 0 deletions app/Http/Controllers/v1/Actions/StoreCanvasUsersAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Http\Controllers\v1\Actions;

use App\Enums\UserRole;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\ProviderPlatform;
use App\Models\ProviderUserMapping;
use App\Models\User;
use Illuminate\Http\Request;

class StoreCanvasUsersAction extends Controller
{
public function __invoke(Request $request)
{
$provider = ProviderPlatform::findOrFail($request->provider_platform_id);
$canvas = $provider->getCanvasServices();
$users = $canvas->listUsers();
$new_users = [];
foreach ($users as $user) {
// Check if we have the user already by the mapping so we have a unique key
if ($provider->providerUserMappings()->where('external_user_id', $user['id'])->exists()) {
continue;
}
$user_profile = $canvas->getUserProfile($user['id']);
$v2_user = User::create([
'username' => $user['login_id'],
'email' => $user_profile['email'],
'name_first' => $user_profile['first_name'],
'name_last' => $user_profile['last_name'],
'role' => UserRole::STUDENT,
'password' => bcrypt('ChangeMe!'),
'password_reset' => true,
]);
$new_users[] = $v2_user;
ProviderUserMapping::create([
'user_id' => $v2_user->id,
'provider_platform_id' => $provider->id,
'external_user_id' => $user['id'],
'external_username' => $user_profile['name'],
]);
}

return UserResource::collection($new_users);
}
}
25 changes: 21 additions & 4 deletions app/Http/Controllers/v1/ProviderPlatformController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Requests\AdminRequest;
use App\Http\Requests\StoreProviderPlatformRequest;
use App\Http\Requests\UpdateProviderPlatformRequest;
use App\Http\Resources\ProviderPlatformAccessKeyResource;
use App\Http\Resources\ProviderPlatformResource;
use App\Models\ProviderPlatform;

Expand Down Expand Up @@ -35,8 +36,9 @@ public function store(StoreProviderPlatformRequest $request)
'errors' => $th->getMessage(),
], 422);
}
$newProviderPlatform = ProviderPlatform::create($providerPlatform);
// $newProviderPlatform->hashAccessKey();
$newProviderPlatform = new ProviderPlatform($providerPlatform);
$newProviderPlatform->encryptAccessKey($request['access_key']);
$newProviderPlatform->save();

return ProviderPlatformResource::make($newProviderPlatform);
}
Expand All @@ -46,15 +48,26 @@ public function store(StoreProviderPlatformRequest $request)
*/
public function show(AdminRequest $request, string $id)
{
$show_key = $request->query('show_key', false);
$request->authorize();
$provider_platform = ProviderPlatform::findOrFail($id);
if (! $show_key) {
return ProviderPlatformResource::make($provider_platform);
}
$key = $provider_platform->access_key;
try {
$provider_platform->access_key = $provider_platform->decryptAccessKey();
} catch (\Illuminate\Contracts\Encryption\DecryptException $e) {
$provider_platform->access_key = $key;
}

return ProviderPlatformResource::make(ProviderPlatform::findOrFail($id));
return ProviderPlatformAccessKeyResource::make($provider_platform);
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateProviderPlatformRequest $request, int $id)
public function update(UpdateProviderPlatformRequest $request, string $id)
{
try {
$validated = $request->validated();
Expand All @@ -65,6 +78,10 @@ public function update(UpdateProviderPlatformRequest $request, int $id)
], 422);
}
$providerPlatform = ProviderPlatform::findOrFail($id);
if (array_key_exists('access_key', $validated) && ! is_null($validated['access_key'])) {
$providerPlatform->encryptAccessKey($validated['access_key']);
$providerPlatform->save();
}
$providerPlatform->update($validated);

return ProviderPlatformResource::make($providerPlatform);
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/v1/UserActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public function index(Request $request)
$sortOrder = $request->query('order', 'asc');
$search = $request->query('search', '');

$query = UserActivity::query()->join('users', 'users.id', '=', 'user_activities.user_id');
$query = UserActivity::query()->join('users', 'user_activities.user_id', '=', 'users.id')
->select('user_activities.*', 'users.name_first', 'users.name_last');
$query->orderBy('user_activities.created_at', $sortOrder);

// Apply search
if ($search !== null) {
$query->where(function ($query) use ($search) {
Expand All @@ -29,7 +29,6 @@ public function index(Request $request)
->orWhere('clicked_url', 'like', '%'.$search.'%');
});
}

// Check if the user is an admin
if ($request->user()->isAdmin()) {
$query->orderBy($sortBy, $sortOrder);
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/v1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function index(AdminRequest $request)
*/
public function store(StoreUserRequest $request)
{
$role = match ($request->role) {
'admin' => 'admin',
'student' => 'student',
default => 'student',
};
try {
$user = $request->validated();
} catch (\Throwable $th) {
Expand All @@ -55,6 +60,7 @@ public function store(StoreUserRequest $request)
], 422);
}
$newUser = new User($user);
$newUser->role = $role;
$pw = $newUser->createTempPassword();

return response(NewUserResource::withPassword($newUser, $pw), 201);
Expand Down
61 changes: 61 additions & 0 deletions app/Http/Controllers/v1/UserCourseActivityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Http\Controllers\v1;

use App\Http\Controllers\Controller;
use App\Http\Resources\UserCourseActivityResource;
use App\Models\UserCourseActivity;
use Illuminate\Http\Request;

class UserCourseActivityController extends Controller
{
/*
* GET /api/v1/users/{id}/course-activity
* Get user course activity
*/
public function index(Request $request, $id)
{
if ($request->user()->id != $id && ! $request->user()->isAdmin()) {
return response()->json(['Message' => 'Unauthorized'], 403);
}
$perPage = request()->query('per_page', 10);
$sortBy = request()->query('sort', 'date');
$sortOrder = request()->query('order', 'asc');
$search = request()->query('search', '');
$query = UserCourseActivity::where('user_id', $id)->with('enrollment');
if ($search) {
$query->where(function ($query) use ($search) {
$query->where('external_course_name', 'like', '%'.$search.'%');
});
}
$query->orderBy($sortBy, $sortOrder);

$users = $query->paginate($perPage);

return UserCourseActivityResource::collection($users);
}

/*
* GET /api/v1/users/{id}/course-activity/{courseId}
* Get user course activity by course id
*/
public function show($user_id, $course_id, Request $request)
{
if (! $request->user()->isAdmin() && $user_id != $request->user()->id) {
return response()->json(['message' => 'Unauthorized to view this user'], 403);
}
$perPage = request()->query('per_page', 10);
$sortBy = request()->query('sort', 'date');
$sortOrder = request()->query('order', 'asc');

$query = UserCourseActivity::where(['user_id' => $user_id])->whereHas('enrollment', function ($query) use ($course_id) {
$query->where('course_id', $course_id);
});

$query->orderBy($sortBy, $sortOrder);

$users = $query->paginate($perPage);

return UserCourseActivityResource::collection($users);
}
}
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreProviderPlatformRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function rules(): array
'type' => [Rule::enum(ProviderPlatformType::class)],
'description' => 'nullable|string|max:255',
'icon_url' => 'nullable|url:http,https',
'account_id' => 'required|unique:provider_platforms,account_id',
'access_key' => 'required|unique:provider_platforms,access_key',
'account_id' => 'required|integer',
'access_key' => 'required|string|max:255',
'base_url' => 'required|url:http,https',
'state' => [Rule::enum(ProviderPlatformState::class)],
];
Expand Down
12 changes: 7 additions & 5 deletions app/Http/Requests/StoreUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Http\Requests;

use App\Enums\UserRole;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreUserRequest extends FormRequest
{
Expand All @@ -22,11 +24,11 @@ public function authorize(): bool
public function rules(): array
{
return [
'name_first' => 'required|string|max:25',
'name_last' => 'required|string|max:25',
'email' => 'nullable|email|max:75|unique:users',
'username' => 'required|string|max:50|unique:users',
'role' => 'required|string|in:student,admin',
'name_first' => 'required|string|max:35',
'name_last' => 'required|string|max:35',
'email' => 'nullable|email|max:90|unique:users',
'username' => 'required|string|max:70|unique:users',
'role' => [Rule::Enum(UserRole::class)],
];
}
}
12 changes: 4 additions & 8 deletions app/Http/Requests/UpdateProviderPlatformRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace App\Http\Requests;

use App\Enums\ProviderPlatformState;
use App\Enums\ProviderPlatformType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateProviderPlatformRequest extends FormRequest
{
Expand All @@ -26,14 +23,13 @@ public function rules(): array
{
return [
'name' => 'nullable|string|max:255',
'type' => 'nullable',
'type' => [Rule::enum(ProviderPlatformType::class)],
'type' => 'nullable|string',
'description' => 'nullable|string|max:255',
'icon_url' => 'nullable|url:http,https',
'account_id' => 'nullable',
'access_key' => 'nullable',
'account_id' => 'nullable|int',
'access_key' => 'nullable|string|max:255',
'base_url' => 'nullable|url:http,https',
'state' => [Rule::enum(ProviderPlatformState::class)],
'state' => 'nullable|string',
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/ProviderPlatformAccessKeyResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ProviderPlatformAccessKeyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}
3 changes: 1 addition & 2 deletions app/Http/Resources/ProviderPlatformResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class ProviderPlatformResource extends JsonResource
*/
public function toArray(Request $request): array
{
// return parent::toArray($request);
return [
'id' => $this->id,
'type' => $this->type,
'name' => $this->name,
'description' => $this->description,
'icon_url' => $this->icon_url,
// don't return an access key unless they call 'show' with the id
'account_id' => $this->account_id,
'access_key' => $this->access_key,
'base_url' => $this->base_url,
'state' => $this->state,
];
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Resources/UserCourseActivityResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserCourseActivityResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'user_id' => $this->user_id,
'username' => $this->user->username,
'course_name' => $this->enrollment->course->external_course_name,
'enrollment_id' => $this->enrollment_id,
'total_time' => $this->external_total_activity_time,
'has_activity' => $this->external_has_activity,
'date' => $this->date,
'provider_platform_id' => $this->enrollment->course->provider_platform_id,
];
}
}
Loading
Loading