Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/SignupTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Http\Controllers\API\SignupController; | use App\Http\Controllers\API\SignupController; | ||||
use App\Discount; | use App\Discount; | ||||
use App\Domain; | use App\Domain; | ||||
use App\SignupCode; | use App\SignupCode; | ||||
use App\SignupInvitation as SI; | |||||
use App\User; | use App\User; | ||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class SignupTest extends TestCase | class SignupTest extends TestCase | ||||
{ | { | ||||
private $domain; | private $domain; | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
// TODO: Some tests depend on existence of individual and group plans, | // TODO: Some tests depend on existence of individual and group plans, | ||||
// we should probably create plans here to not depend on that | // we should probably create plans here to not depend on that | ||||
$this->domain = $this->getPublicDomain(); | $this->domain = $this->getPublicDomain(); | ||||
$this->deleteTestUser("SignupControllerTest1@$this->domain"); | $this->deleteTestUser("SignupControllerTest1@$this->domain"); | ||||
$this->deleteTestUser("signuplogin@$this->domain"); | $this->deleteTestUser("signuplogin@$this->domain"); | ||||
$this->deleteTestUser("admin@external.com"); | $this->deleteTestUser("admin@external.com"); | ||||
$this->deleteTestUser("test-inv@kolabnow.com"); | |||||
$this->deleteTestDomain('external.com'); | $this->deleteTestDomain('external.com'); | ||||
$this->deleteTestDomain('signup-domain.com'); | $this->deleteTestDomain('signup-domain.com'); | ||||
$this->deleteTestGroup('group-test@kolabnow.com'); | $this->deleteTestGroup('group-test@kolabnow.com'); | ||||
SI::truncate(); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser("SignupControllerTest1@$this->domain"); | $this->deleteTestUser("SignupControllerTest1@$this->domain"); | ||||
$this->deleteTestUser("signuplogin@$this->domain"); | $this->deleteTestUser("signuplogin@$this->domain"); | ||||
$this->deleteTestUser("admin@external.com"); | $this->deleteTestUser("admin@external.com"); | ||||
$this->deleteTestUser("test-inv@kolabnow.com"); | |||||
$this->deleteTestDomain('external.com'); | $this->deleteTestDomain('external.com'); | ||||
$this->deleteTestDomain('signup-domain.com'); | $this->deleteTestDomain('signup-domain.com'); | ||||
$this->deleteTestGroup('group-test@kolabnow.com'); | $this->deleteTestGroup('group-test@kolabnow.com'); | ||||
SI::truncate(); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Return a public domain for signup tests | * Return a public domain for signup tests | ||||
*/ | */ | ||||
private function getPublicDomain(): string | private function getPublicDomain(): string | ||||
Show All 13 Lines | private function getPublicDomain(): string | ||||
} | } | ||||
} | } | ||||
return $this->domain; | return $this->domain; | ||||
} | } | ||||
/** | /** | ||||
* Test fetching plans for signup | * Test fetching plans for signup | ||||
* | |||||
* @return void | |||||
*/ | */ | ||||
public function testSignupPlans() | public function testSignupPlans(): void | ||||
{ | { | ||||
$response = $this->get('/api/auth/signup/plans'); | $response = $this->get('/api/auth/signup/plans'); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertCount(2, $json['plans']); | $this->assertCount(2, $json['plans']); | ||||
$this->assertArrayHasKey('title', $json['plans'][0]); | $this->assertArrayHasKey('title', $json['plans'][0]); | ||||
$this->assertArrayHasKey('name', $json['plans'][0]); | $this->assertArrayHasKey('name', $json['plans'][0]); | ||||
$this->assertArrayHasKey('description', $json['plans'][0]); | $this->assertArrayHasKey('description', $json['plans'][0]); | ||||
$this->assertArrayHasKey('button', $json['plans'][0]); | $this->assertArrayHasKey('button', $json['plans'][0]); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching invitation | |||||
*/ | |||||
public function testSignupInvitations(): void | |||||
{ | |||||
Queue::fake(); | |||||
$invitation = SI::create(['email' => 'email1@ext.com']); | |||||
// Test existing invitation | |||||
$response = $this->get("/api/auth/signup/invitations/{$invitation->id}"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame($invitation->id, $json['id']); | |||||
// Test non-existing invitation | |||||
$response = $this->get("/api/auth/signup/invitations/abc"); | |||||
$response->assertStatus(404); | |||||
// Test completed invitation | |||||
SI::where('id', $invitation->id)->update(['status' => SI::STATUS_COMPLETED]); | |||||
$response = $this->get("/api/auth/signup/invitations/{$invitation->id}"); | |||||
$response->assertStatus(404); | |||||
} | |||||
/** | |||||
* Test signup initialization with invalid input | * Test signup initialization with invalid input | ||||
* | |||||
* @return void | |||||
*/ | */ | ||||
public function testSignupInitInvalidInput() | public function testSignupInitInvalidInput(): void | ||||
{ | { | ||||
// Empty input data | // Empty input data | ||||
$data = []; | $data = []; | ||||
$response = $this->post('/api/auth/signup/init', $data); | $response = $this->post('/api/auth/signup/init', $data); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | public function testSignupInitInvalidInput(): void | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$this->assertArrayHasKey('voucher', $json['errors']); | $this->assertArrayHasKey('voucher', $json['errors']); | ||||
// TODO: Test phone validation | // TODO: Test phone validation | ||||
} | } | ||||
/** | /** | ||||
* Test signup initialization with valid input | * Test signup initialization with valid input | ||||
* | |||||
* @return array | |||||
*/ | */ | ||||
public function testSignupInitValidInput() | public function testSignupInitValidInput(): array | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
// Assert that no jobs were pushed... | // Assert that no jobs were pushed... | ||||
Queue::assertNothingPushed(); | Queue::assertNothingPushed(); | ||||
$data = [ | $data = [ | ||||
'email' => 'testuser@external.com', | 'email' => 'testuser@external.com', | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | public function testSignupInitValidInput(): array | ||||
'voucher' => $data['voucher'] | 'voucher' => $data['voucher'] | ||||
]; | ]; | ||||
} | } | ||||
/** | /** | ||||
* Test signup code verification with invalid input | * Test signup code verification with invalid input | ||||
* | * | ||||
* @depends testSignupInitValidInput | * @depends testSignupInitValidInput | ||||
* @return void | |||||
*/ | */ | ||||
public function testSignupVerifyInvalidInput(array $result) | public function testSignupVerifyInvalidInput(array $result): void | ||||
{ | { | ||||
// Empty data | // Empty data | ||||
$data = []; | $data = []; | ||||
$response = $this->post('/api/auth/signup/verify', $data); | $response = $this->post('/api/auth/signup/verify', $data); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
Show All 31 Lines | public function testSignupVerifyInvalidInput(array $result): void | ||||
// TODO: Test expired code | // TODO: Test expired code | ||||
} | } | ||||
/** | /** | ||||
* Test signup code verification with valid input | * Test signup code verification with valid input | ||||
* | * | ||||
* @depends testSignupInitValidInput | * @depends testSignupInitValidInput | ||||
* | |||||
* @return array | |||||
*/ | */ | ||||
public function testSignupVerifyValidInput(array $result) | public function testSignupVerifyValidInput(array $result): array | ||||
{ | { | ||||
$code = SignupCode::find($result['code']); | $code = SignupCode::find($result['code']); | ||||
$data = [ | $data = [ | ||||
'code' => $code->code, | 'code' => $code->code, | ||||
'short_code' => $code->short_code, | 'short_code' => $code->short_code, | ||||
]; | ]; | ||||
$response = $this->post('/api/auth/signup/verify', $data); | $response = $this->post('/api/auth/signup/verify', $data); | ||||
Show All 11 Lines | public function testSignupVerifyValidInput(array $result): array | ||||
return $result; | return $result; | ||||
} | } | ||||
/** | /** | ||||
* Test last signup step with invalid input | * Test last signup step with invalid input | ||||
* | * | ||||
* @depends testSignupVerifyValidInput | * @depends testSignupVerifyValidInput | ||||
* @return void | |||||
*/ | */ | ||||
public function testSignupInvalidInput(array $result) | public function testSignupInvalidInput(array $result): void | ||||
{ | { | ||||
// Empty data | // Empty data | ||||
$data = []; | $data = []; | ||||
$response = $this->post('/api/auth/signup', $data); | $response = $this->post('/api/auth/signup', $data); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | public function testSignupInvalidInput(array $result): void | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$this->assertArrayHasKey('login', $json['errors']); | $this->assertArrayHasKey('login', $json['errors']); | ||||
} | } | ||||
/** | /** | ||||
* Test last signup step with valid input (user creation) | * Test last signup step with valid input (user creation) | ||||
* | * | ||||
* @depends testSignupVerifyValidInput | * @depends testSignupVerifyValidInput | ||||
* @return void | |||||
*/ | */ | ||||
public function testSignupValidInput(array $result) | public function testSignupValidInput(array $result): void | ||||
{ | { | ||||
$queue = Queue::fake(); | $queue = Queue::fake(); | ||||
$domain = $this->getPublicDomain(); | $domain = $this->getPublicDomain(); | ||||
$identity = \strtolower('SignupLogin@') . $domain; | $identity = \strtolower('SignupLogin@') . $domain; | ||||
$code = SignupCode::find($result['code']); | $code = SignupCode::find($result['code']); | ||||
$data = [ | $data = [ | ||||
'login' => 'SignupLogin', | 'login' => 'SignupLogin', | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | public function testSignupValidInput(array $result): void | ||||
// TODO: Check SKUs/Plan | // TODO: Check SKUs/Plan | ||||
// TODO: Check if the access token works | // TODO: Check if the access token works | ||||
} | } | ||||
/** | /** | ||||
* Test signup for a group (custom domain) account | * Test signup for a group (custom domain) account | ||||
* | |||||
* @return void | |||||
*/ | */ | ||||
public function testSignupGroupAccount() | public function testSignupGroupAccount(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
// Initial signup request | // Initial signup request | ||||
$user_data = $data = [ | $user_data = $data = [ | ||||
'email' => 'testuser@external.com', | 'email' => 'testuser@external.com', | ||||
'first_name' => 'Signup', | 'first_name' => 'Signup', | ||||
'last_name' => 'User', | 'last_name' => 'User', | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | public function testSignupGroupAccount(): void | ||||
// TODO: Check domain record | // TODO: Check domain record | ||||
// TODO: Check SKUs/Plan | // TODO: Check SKUs/Plan | ||||
// TODO: Check if the access token works | // TODO: Check if the access token works | ||||
} | } | ||||
/** | /** | ||||
* Test signup via invitation | |||||
*/ | |||||
public function testSignupViaInvitation(): void | |||||
{ | |||||
Queue::fake(); | |||||
$invitation = SI::create(['email' => 'email1@ext.com']); | |||||
$post = [ | |||||
'invitation' => 'abc', | |||||
'first_name' => 'Signup', | |||||
'last_name' => 'User', | |||||
'login' => 'test-inv', | |||||
'domain' => 'kolabnow.com', | |||||
'password' => 'test', | |||||
'password_confirmation' => 'test', | |||||
]; | |||||
// Test invalid invitation identifier | |||||
$response = $this->post('/api/auth/signup', $post); | |||||
$response->assertStatus(404); | |||||
// Test valid input | |||||
$post['invitation'] = $invitation->id; | |||||
$response = $this->post('/api/auth/signup', $post); | |||||
$result = $response->json(); | |||||
$response->assertStatus(200); | |||||
$this->assertSame('success', $result['status']); | |||||
$this->assertSame('bearer', $result['token_type']); | |||||
$this->assertTrue(!empty($result['expires_in']) && is_int($result['expires_in']) && $result['expires_in'] > 0); | |||||
$this->assertNotEmpty($result['access_token']); | |||||
$this->assertSame('test-inv@kolabnow.com', $result['email']); | |||||
// Check if the user has been created | |||||
$user = User::where('email', 'test-inv@kolabnow.com')->first(); | |||||
$this->assertNotEmpty($user); | |||||
// Check user settings | |||||
$this->assertSame($invitation->email, $user->getSetting('external_email')); | |||||
$this->assertSame($post['first_name'], $user->getSetting('first_name')); | |||||
$this->assertSame($post['last_name'], $user->getSetting('last_name')); | |||||
$invitation->refresh(); | |||||
$this->assertSame($user->id, $invitation->user_id); | |||||
$this->assertTrue($invitation->isCompleted()); | |||||
// TODO: Test POST params validation | |||||
} | |||||
/** | |||||
* List of login/domain validation cases for testValidateLogin() | * List of login/domain validation cases for testValidateLogin() | ||||
* | * | ||||
* @return array Arguments for testValidateLogin() | * @return array Arguments for testValidateLogin() | ||||
*/ | */ | ||||
public function dataValidateLogin(): array | public function dataValidateLogin(): array | ||||
{ | { | ||||
$domain = $this->getPublicDomain(); | $domain = $this->getPublicDomain(); | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |