primo upload

This commit is contained in:
claus75a
2024-03-16 20:37:32 +01:00
commit e43b9b4b28
3019 changed files with 406000 additions and 0 deletions
@@ -0,0 +1,83 @@
<?php
namespace Tests\Feature\Api\Profile;
use Facades\Tests\Setup\UserFactory;
use Tests\Feature\ApiTestCase;
class AuthDetailsControllerTest extends ApiTestCase
{
/** @test */
public function user_can_update_his_authentication_details()
{
$user = $this->login();
$this->patch('/api/me/details/auth', [
'email' => 'foo@example.com',
'username' => 'john.doe',
'password' => '12345678',
'password_confirmation' => '12345678'
])->assertOk()
->assertJsonFragment(['email' => 'foo@example.com', 'username' => 'john.doe']);
$this->assertTrue(password_verify('12345678', $user->fresh()->password));
}
/** @test */
public function user_can_update_only_email_and_leave_other_fields_unchanged()
{
$user = $this->login();
$this->patch('/api/me/details/auth', [
'email' => 'foo@example.com',
])->assertOk()
->assertJsonFragment(['email' => 'foo@example.com']);
$this->assertEquals($user->username, $user->fresh()->username);
$this->assertEquals($user->password, $user->fresh()->password);
}
/** @test */
public function email_field_is_required()
{
$this->login();
$this->patch('/api/me/details/auth')
->assertJsonValidationErrors('email');
}
/** @test */
public function email_field_must_be_valid_email()
{
$this->login();
$this->patch('/api/me/details/auth', [
'email' => 'invalid email'
])->assertJsonValidationErrors('email');
}
/** @test */
public function email_field_must_be_unique()
{
$this->login();
UserFactory::email('john.doe@test.com')->create();
$this->patch('/api/me/details/auth', [
'email' => 'john.doe@test.com',
])->assertJsonValidationErrors('email');
}
/** @test */
public function username_field_must_be_unique()
{
$this->login();
UserFactory::withCredentials('john.doe', '123123')->create();
$this->patch('/api/me/details/auth', [
'email' => 'john.doe@test.com',
'username' => 'john.doe'
])->assertJsonValidationErrors('username');
}
}
@@ -0,0 +1,97 @@
<?php
namespace Tests\Feature\Api\Profile;
use Illuminate\Http\UploadedFile;
use Storage;
use Tests\Feature\ApiTestCase;
class AvatarControllerTest extends ApiTestCase
{
/** @test */
public function only_authenticated_user_can_update_avatar()
{
$this->post('/api/me/avatar')->assertStatus(401);
}
/** @test */
public function upload_avatar_image()
{
$this->login();
Storage::fake('public');
$file = UploadedFile::fake()->image('avatar.png', 500, 500);
$response = $this->post('api/me/avatar', [
'file' => $file
]);
$this->assertNotNull($response->json('data.avatar'));
$uploadedFile = str_replace(url(''), '', $response->json('data.avatar'));
$uploadedFile = ltrim($uploadedFile, "/");
Storage::disk('public')->assertExists($uploadedFile);
list($width, $height) = getimagesizefromstring(
Storage::disk('public')->get($uploadedFile)
);
$this->assertEquals(160, $width);
$this->assertEquals(160, $height);
}
/** @test */
public function upload_invalid_image()
{
$this->login();
Storage::fake('public');
$file = UploadedFile::fake()->create('avatar.txt', 500);
$this->post('/api/me/avatar', ['file' => $file])
->assertStatus(422)
->assertJsonFragment([
'file' => [
trans('validation.image', ['attribute' => 'file'])
]
]);
}
/** @test */
public function update_avatar_from_external_source()
{
$this->login();
$url = 'http://google.com';
$this->putJson('/api/me/avatar/external', ['url' => $url])
->assertOk()
->assertJsonFragment(['avatar' => $url]);
}
/** @test */
public function update_avatar_with_invalid_external_source()
{
$this->login();
$this->putJson('/api/me/avatar/external', ['url' => 'foo'])
->assertStatus(422);
}
/** @test */
public function delete_avatar()
{
$user = $this->login();
$user->forceFill(['avatar' => 'http://google.com'])->save();
$this->deleteJson("api/me/avatar")
->assertOk()
->assertJsonFragment([
'avatar' => url('assets/img/profile.png') // default profile image
]);
}
}
@@ -0,0 +1,121 @@
<?php
namespace Tests\Feature\Api\Profile;
use Carbon\Carbon;
use Tests\Feature\ApiTestCase;
use Vanguard\Http\Resources\UserResource;
class DetailsControllerTest extends ApiTestCase
{
/** @test */
public function get_user_profile_unauthenticated()
{
$this->getJson('/api/me')->assertStatus(401);
}
/** @test */
public function get_user_profile()
{
$user = $this->login();
$this->getJson('/api/me')
->assertOk()
->assertJson(['data' => (new UserResource($user))->resolve()]);
}
/** @test */
public function update_user_profile_unauthenticated()
{
$this->patchJson('/api/me/details')->assertStatus(401);
}
/** @test */
public function update_user_profile()
{
$user = $this->login();
$data = $this->getData();
$response = $this->patchJson("/api/me/details", $data);
$transformed = (new UserResource($user->fresh()))->resolve();
$response->assertJsonFragment($transformed);
$this->assertDatabaseHas('users', array_merge($data, ['id' => $user->id]));
}
/** @test */
public function partially_update_user_details()
{
$user = $this->login();
$data = [
'first_name' => 'John',
'last_name' => 'Doe'
];
$response = $this->patchJson("/api/me/details", $data);
$transformed = (new UserResource($user->fresh()))->resolve();
$response->assertJsonFragment($transformed);
$this->assertDatabaseHas('users', array_merge($data, [
'id' => $user->id,
'birthday' => $user->birthday->format('Y-m-d'),
'phone' => $user->phone,
'address' => $user->address,
'country_id' => $user->country_id,
]));
}
/** @test */
public function update_without_country_id()
{
$user = $this->login();
$data = $this->getData();
unset($data['country_id']);
$response = $this->patchJson("/api/me/details", $data);
$transformed = (new UserResource($user->fresh()))->resolve();
$response->assertJsonFragment($transformed);
$this->assertDatabaseHas('users', array_merge($data, ['id' => $user->id]));
}
/** @test */
public function update_with_invalid_date_format()
{
$this->login();
$this->patchJson("/api/me/details", ['birthday' => 'foo'])
->assertStatus(422)
->assertJsonFragment([
'birthday' => [
trans('validation.date', ['attribute' => 'birthday'])
]
]);
}
/**
* @param array $attrs
* @return array
*/
private function getData(array $attrs = [])
{
return array_merge([
'first_name' => 'John',
'last_name' => 'Doe',
'birthday' => Carbon::now()->subYears(25)->format('Y-m-d'),
'phone' => '(123) 456 789',
'address' => 'some address 1',
'country_id' => 688,
], $attrs);
}
}
@@ -0,0 +1,66 @@
<?php
namespace Tests\Feature\Api\Profile;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Tests\Feature\ApiTestCase;
use Vanguard\Http\Resources\SessionResource;
use Vanguard\Repositories\Session\SessionRepository;
use Vanguard\User;
class SessionsControllerTest extends ApiTestCase
{
/** @test */
public function get_user_sessions_unauthenticated()
{
$this->getJson('/api/me/sessions')->assertStatus(401);
}
/** @test */
public function get_sessions_if_non_database_driver_is_used()
{
config(['session.driver' => 'array']);
$this->login();
$this->getJson('/api/me/sessions')->assertStatus(404);
}
/** @test */
public function get_user_sessions()
{
config(['session.driver' => 'database']);
$user = $this->login();
$sessions = $this->generateNonExpiredSessions($user);
$this->getJson('/api/me/sessions')
->assertOk()
->assertJsonFragment([
'data' => SessionResource::collection($sessions)->resolve()
]);
}
private function generateNonExpiredSessions(User $user, $count = 5)
{
$sessions = [];
$faker = $this->app->make(\Faker\Generator::class);
for ($i = 0; $i < $count; $i++) {
array_push($sessions, [
'id' => Str::random(40),
'user_id' => $user->id,
'ip_address' => $faker->ipv4,
'user_agent' => $faker->userAgent,
'payload' => Str::random(),
'last_activity' => Carbon::now()->subMinute()->timestamp
]);
}
\DB::table('sessions')->insert($sessions);
return app(SessionRepository::class)->getUserSessions($user->id);
}
}
@@ -0,0 +1,163 @@
<?php
namespace Tests\Feature\Api\Profile;
use Authy;
use Event;
use Mockery;
use Tests\Feature\ApiTestCase;
use Vanguard\Events\User\TwoFactorEnabled;
use Vanguard\Http\Resources\UserResource;
use Vanguard\User;
class TwoFactorControllerTest extends ApiTestCase
{
/** @test */
public function update_2fa_unathenticated()
{
$this->setSettings(['2fa.enabled' => true]);
User::factory()->create();
$this->putJson("api/me/2fa")
->assertStatus(401);
}
/** @test */
public function enable_two_factor_auth()
{
$this->setSettings(['2fa.enabled' => true]);
$this->withoutExceptionHandling();
Event::fake([
TwoFactorEnabled::class,
]);
$user = $this->login();
Authy::shouldReceive('isEnabled')->andReturn(false);
Authy::shouldReceive('register')->andReturnNull();
Authy::shouldReceive('sendTwoFactorVerificationToken');
$data = ['country_code' => '1', 'phone_number' => '123'];
$this->putJson("api/me/2fa", $data)
->assertOk()
->assertJson(['message' => 'Verification token sent.']);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'two_factor_country_code' => $data['country_code'],
'two_factor_phone' => $data['phone_number']
]);
Event::assertNotDispatched(TwoFactorEnabled::class);
}
/** @test */
public function verify_user_phone_with_correct_token()
{
$this->setSettings(['2fa.enabled' => true]);
Event::fake([
TwoFactorEnabled::class,
]);
$user = $this->login();
Authy::shouldReceive('isEnabled')->andReturn(false);
Authy::shouldReceive('tokenIsValid')->with(Mockery::any(), '123123')->andReturn(true);
$response = $this->postJson("api/me/2fa/verify", ['token' => '123123']);
$updatedUser = (new UserResource($user->fresh()))->resolve();
$response->assertOk()
->assertJsonFragment($updatedUser);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'two_factor_options' => '{"enabled":true}'
]);
Event::assertDispatched(TwoFactorEnabled::class);
}
/** @test */
public function verify_user_phone_with_invalid_token()
{
$this->setSettings(['2fa.enabled' => true]);
$user = $this->login();
Authy::shouldReceive('isEnabled')->andReturn(false);
Authy::shouldReceive('tokenIsValid')->andReturn(false);
$this->postJson("api/me/2fa/verify", ['token' => '123123'])
->assertStatus(422)
->assertJson(['message' => 'Invalid 2FA token.']);
$this->assertDatabaseMissing('users', [
'id' => $user->id,
'two_factor_options' => '{"enabled":true}'
]);
}
/** @test */
public function enable_two_factor_auth_when_it_is_already_enabled()
{
$this->setSettings(['2fa.enabled' => true]);
$this->login();
Authy::shouldReceive('isEnabled')->andReturn(true);
$data = ['country_code' => '1', 'phone_number' => '123'];
$this->putJson("api/me/2fa", $data)
->assertStatus(422)
->assertJson([
'message' => '2FA is already enabled for this user.'
]);
}
/** @test */
public function disable_two_factor_auth()
{
$this->setSettings(['2fa.enabled' => true]);
$user = User::factory()->create([
'two_factor_country_code' => '1',
'two_factor_phone' => '123'
]);
$this->be($user, self::API_GUARD);
Authy::shouldReceive('isEnabled')->andReturn(true);
Authy::shouldReceive('delete')->andReturnNull();
$response = $this->deleteJson("api/me/2fa");
$user = (new UserResource($user->fresh()))->resolve();
$response->assertOk()
->assertJsonFragment($user);
}
/** @test */
public function disable_2fa_when_it_is_already_disabled()
{
$this->setSettings(['2fa.enabled' => true]);
$this->login();
Authy::shouldReceive('isEnabled')->andReturn(false);
$this->deleteJson("api/me/2fa")
->assertStatus(422)
->assertJson([
'message' => '2FA is not enabled for this user.'
]);
}
}