Guest User Unit Test
user_6716235
php
a month ago
18 kB
3
Indexable
Never
<?php namespace Tests\Feature; use App\Enums\AttributeType; use App\Models\Attribute; use App\Models\AttributeValue; use App\Models\Currency; use App\Models\Domain; use App\Models\ServiceCategory; use App\Models\Skill; use Carbon\Carbon; use Tests\TestCase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; class GuestUserTest extends TestCase { /** * A basic feature test to verify guest user login functionality. * * This test checks if a guest user can successfully log in by making a GET request * to the '/api/v1/guest/login' endpoint. It then validates the response structure and * content to ensure that the login process is functioning correctly. * * Steps: * 1. Make a GET request to the '/api/v1/guest/login' endpoint with 'Accept' header set to 'application/json'. * 2. Decode the JSON response to an associative array. * 3. Assert that the 'success' field in the response is true. * 4. Assert that the 'access_token' field in the response is not empty. * 5. Assert that the 'refresh_token' field in the response is not empty. * * @return void */ public function test_guest_user_login() { // Make a GET request to the guest login endpoint with JSON headers $response = $this->withHeaders([ 'Accept' => 'application/json', ])->get('/api/v1/guest/login'); // Decode the JSON response content to an associative array $response = json_decode($response->content(), true); // Assert that the 'success' field is true, indicating a successful login $this->assertEquals($response['success'], true); // Assert that the 'access_token' is not empty, ensuring it is present in the response $this->assertNotEmpty($response['access_token']); // Assert that the 'refresh_token' is not empty, ensuring it is present in the response $this->assertNotEmpty($response['refresh_token']); return $response; } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_service_category(array $userData) { // Retrieve the first ServiceCategory record from the guest database. $serviceCategory = ServiceCategory::first(); // Retrieve the first Currency record from the guest database. $currency = Currency::first(); // Retrieve the first Domain record from the guest database. $domain = Domain::first(); // Retrieve the IDs of the first two skills related to the service category from the guest database. $skills = Skill::where('service_category_id', $serviceCategory->id) ->limit(2) ->pluck('id') ->toArray(); // Make a POST request to the '/api/v1/service-categories/{id}/attach' endpoint // to attach a service category to the guest user with additional details. // The request includes headers for 'Accept' and 'Authorization', using the access token // obtained from the previous test (test_guest_user_login). $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/service-categories/' . $serviceCategory->id . '/attach', [ 'description' => 'Over 10,000 flight hours', // Description of the service category 'currency_type' => $currency->id, // ID of the currency type 'rate' => 10, // Rate for the service 'domain_id' => $domain->id, // ID of the domain 'latitude' => '23.783731', // Latitude for the location 'longitude' => '90.417441', // Longitude for the location 'radius' => '100', // Radius of the service availability 'is_published' => 1, // Indicates if the service is published 'skills' => $skills // Array of skill IDs related to the service category ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_surname(array $userData) { // Make a POST request to the '/api/v1/attributes/attach_surname' endpoint // to attach a surname to the guest user's profile. // The request includes headers for 'Accept' and 'Authorization', using the access token // obtained from the previous test (test_guest_user_login). $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/attributes/attach_surname', [ 'first_name' => 'John', // Guest user's first name 'last_name' => 'Doe', // Guest user's last name (surname) 'is_visible_on_profile' => 1 // Flag indicating if the surname is visible on the profile ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_unit_attribute(array $userData) { // Retrieve the 'Weight' attribute from the guest database. // AttributeType::Weight->value is assumed to return the ID of the 'Weight' attribute. $attribute = Attribute::where('id', AttributeType::Weight->value) ->first(); // Make a POST request to the '/api/v1/attributes/attach_unit_value' endpoint // to attach a unit value (e.g., weight) to the guest user's profile. // The request includes headers for 'Accept' and 'Authorization', using the access token // obtained from the previous test (test_guest_user_login). $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/attributes/attach_unit_value', [ 'attributes_id' => $attribute->id, // ID of the 'Weight' attribute 'unit' => 'Kg', // Unit of measurement (Kilograms) 'value' => '60', // Value associated with the unit (e.g., 60 Kg) 'is_visible_on_profile' => 1 // Flag indicating if this attribute is visible on the profile ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_social_link(array $userData) { // Retrieve the first service category from the guest database. $serviceCategory = ServiceCategory::first(); // Retrieve the attribute related to social links from the guest database. $attributes = Attribute::where('id', AttributeType::SocialLinks->value)->first(); // Retrieve the IDs of the social link attributes from the guest database. $attributeValue = AttributeValue::where('attributes_id', $attributes->id) ->whereIn('value', ['Facebook', 'Github']) ->pluck('id') ->toArray(); // Make a POST request to the '/api/v1/attributes/attach_social_links' endpoint // to attach social links to a service category in the guest user's profile. $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/attributes/attach_social_links', [ 'service_category_id' => $serviceCategory->id, // ID of the service category 'attribute_value_id_list' => $attributeValue, // List of attribute value IDs (e.g., Facebook, Github) 'values' => ['John', 'Doe'], // Social links to be attached 'is_visible_on_profile' => 1 // Flag indicating if the links should be visible on the profile ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_qualification(array $userData) { // Retrieve the first service category from the guest database. $serviceCategory = ServiceCategory::first(); // Make a POST request to the '/api/v1/attributes/attach_qualifications' endpoint // to attach qualifications to a service category in the guest user's profile. $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/attributes/attach_qualifications', [ 'service_category_id' => $serviceCategory->id, // ID of the service category 'highlight_of_qualification' => ['Qualification1', 'Qualification2'], // List of qualifications 'is_visible_on_profile' => 1 // Flag indicating if the qualifications should be visible on the profile ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * @depends test_guest_user_login */ public function test_guest_user_add_attachment(array $userData) { // Prepare the file to be uploaded $file = UploadedFile::fake()->create('sample.pdf', 100); // 100 KB file // Fake the public disk for testing Storage::fake('public'); // Retrieve the attribute by ID (ensure this is the correct ID and attribute) $attribute = Attribute::where('id', AttributeType::CreditScore->value) ->firstOrFail(); // Make the POST request with the file attachment $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/attributes/attach_attachment', [ 'attributes_id' => $attribute->id, 'attach_file' => $file, 'value' => '40', 'unit' => 'USD', 'is_visible_on_profile' => 1 ]); // Decode the JSON response content to an associative array for easier assertions $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success' $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_project(array $userData) { // Retrieve the first service category from the guest database. $serviceCategory = ServiceCategory::first(); // Make a POST request to the '/api/v1/attributes/attach_projects' endpoint // to attach projects to a service category in the guest user's profile. $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/attributes/attach_projects', [ 'service_category_id' => $serviceCategory->id, // ID of the service category 'projects' => ['Project1', 'Project2'], // List of projects 'is_visible_on_profile' => 1 // Flag indicating if the projects should be visible on the profile ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_education(array $userData) { // Make a POST request to the '/api/v1/educations' endpoint to add education details. // The request includes headers for 'Accept' and 'Authorization', using the access token // obtained from the previous test (test_guest_user_login). $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/educations', [ 'school' => 'University of Oxford', // Name of the school 'field_of_study' => 'Computer Science', // Field of study 'starting_date' => Carbon::now()->subYears(5)->format('Y-m-d'), // Starting date, 5 years ago from now 'ending_date' => Carbon::now()->subYears(1)->format('Y-m-d'), // Ending date, also 1 years ago (same as starting) ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } /** * This test depends on the successful execution of the test_guest_user_login method. * It uses the response (specifically the access token) from the login test. * * @depends test_guest_user_login */ public function test_guest_user_add_experience(array $userData) { $serviceCategory = ServiceCategory::first(); // Make a POST request to the '/api/v1/service-categories/{id}/experiences' endpoint // to add an experience under the first service category. // The request includes headers for 'Accept' and 'Authorization', using the access token // obtained from the previous test (test_guest_user_login). $response = $this->withHeaders([ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $userData['access_token'], ])->post('/api/v1/service-categories/' . $serviceCategory['id'] . '/experiences', [ 'organization' => 'Oracle Corporation', // Name of the organization 'position' => 'Laravel Developer', // Position held at the organization 'location' => 'Austin, Texas, United States', // Location of the organization 'starting_date' => Carbon::now()->subYears(5)->format('Y-m-d'), // Start date, 5 years ago 'ending_date' => Carbon::now()->subYears(3)->format('Y-m-d'), // End date, 3 years ago 'responsibilities' => ['Develop Api', 'Unit Testing'] // List of responsibilities ]); // Decode the JSON response content to an associative array for easier assertions. $response = json_decode($response->content(), true); // Assert that the response contains a 'status' key. $this->assertArrayHasKey('status', $response); // Assert that the 'status' key in the response has a value of 'Success'. $this->assertEquals('Success', $response['status']); } }
Leave a Comment