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\Domain; | use App\Domain; | ||||
use App\SignupCode; | use App\SignupCode; | ||||
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 static $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 | ||||
$domain = self::getPublicDomain(); | $this->domain = $this->getPublicDomain(); | ||||
$user = $this->getTestUser("SignupControllerTest1@$domain"); | |||||
$this->deleteTestUser("SignupControllerTest1@$this->domain"); | |||||
$this->deleteTestUser("signuplogin@$this->domain"); | |||||
$this->deleteTestUser("admin@external.com"); | |||||
$this->deleteTestDomain('external.com'); | |||||
$this->deleteTestDomain('signup-domain.com'); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$domain = self::getPublicDomain(); | $this->deleteTestUser("SignupControllerTest1@$this->domain"); | ||||
$this->deleteTestUser("signuplogin@$this->domain"); | |||||
$this->deleteTestUser("admin@external.com"); | |||||
User::where('email', "signuplogin@$domain") | $this->deleteTestDomain('external.com'); | ||||
->orWhere('email', "SignupControllerTest1@$domain") | $this->deleteTestDomain('signup-domain.com'); | ||||
->orWhere('email', 'admin@external.com') | |||||
->delete(); | parent::tearDown(); | ||||
Domain::where('namespace', 'signup-domain.com') | |||||
->orWhere('namespace', 'external.com') | |||||
->delete(); | |||||
} | } | ||||
/** | /** | ||||
* Return a public domain for signup tests | * Return a public domain for signup tests | ||||
*/ | */ | ||||
public function getPublicDomain(): string | private function getPublicDomain(): string | ||||
{ | { | ||||
if (!self::$domain) { | if (!$this->domain) { | ||||
$this->refreshApplication(); | $this->refreshApplication(); | ||||
$public_domains = Domain::getPublicDomains(); | $public_domains = Domain::getPublicDomains(); | ||||
self::$domain = reset($public_domains); | $this->domain = reset($public_domains); | ||||
if (empty(self::$domain)) { | if (empty($this->domain)) { | ||||
self::$domain = 'signup-domain.com'; | $this->domain = 'signup-domain.com'; | ||||
Domain::create([ | Domain::create([ | ||||
'namespace' => self::$domain, | 'namespace' => $this->domain, | ||||
'status' => Domain::STATUS_ACTIVE, | 'status' => Domain::STATUS_ACTIVE, | ||||
'type' => Domain::TYPE_PUBLIC, | 'type' => Domain::TYPE_PUBLIC, | ||||
]); | ]); | ||||
} | } | ||||
} | } | ||||
return self::$domain; | return $this->domain; | ||||
} | } | ||||
/** | /** | ||||
* Test fetching plans for signup | * Test fetching plans for signup | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function testSignupPlans() | public function testSignupPlans() | ||||
▲ Show 20 Lines • Show All 338 Lines • ▼ Show 20 Lines | public function testSignupValidInput(array $result) | ||||
Queue::assertPushed(\App\Jobs\UserCreate::class, 1); | Queue::assertPushed(\App\Jobs\UserCreate::class, 1); | ||||
Queue::assertPushed(\App\Jobs\UserCreate::class, function ($job) use ($data) { | Queue::assertPushed(\App\Jobs\UserCreate::class, function ($job) use ($data) { | ||||
$job_user = TestCase::getObjectProperty($job, 'user'); | $job_user = TestCase::getObjectProperty($job, 'user'); | ||||
return $job_user->email === \strtolower($data['login'] . '@' . $data['domain']); | return $job_user->email === \strtolower($data['login'] . '@' . $data['domain']); | ||||
}); | }); | ||||
// Check if the code has been removed | // Check if the code has been removed | ||||
$this->assertNull(SignupCode::where($result['code'])->first()); | $this->assertNull(SignupCode::where('code', $result['code'])->first()); | ||||
// Check if the user has been created | // Check if the user has been created | ||||
$user = User::where('email', $identity)->first(); | $user = User::where('email', $identity)->first(); | ||||
$this->assertNotEmpty($user); | $this->assertNotEmpty($user); | ||||
$this->assertSame($identity, $user->email); | $this->assertSame($identity, $user->email); | ||||
$this->assertSame($result['name'], $user->name); | $this->assertSame($result['name'], $user->name); | ||||
Show All 16 Lines | public function testSignupGroupAccount() | ||||
// Initial signup request | // Initial signup request | ||||
$user_data = $data = [ | $user_data = $data = [ | ||||
'email' => 'testuser@external.com', | 'email' => 'testuser@external.com', | ||||
'name' => 'Signup User', | 'name' => 'Signup User', | ||||
'plan' => 'group', | 'plan' => 'group', | ||||
]; | ]; | ||||
$response = $this->post('/api/auth/signup/init', $data); | $response = $this->withoutMiddleware()->post('/api/auth/signup/init', $data); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertCount(2, $json); | $this->assertCount(2, $json); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertNotEmpty($json['code']); | $this->assertNotEmpty($json['code']); | ||||
// Assert the email sending job was pushed once | // Assert the email sending job was pushed once | ||||
▲ Show 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | public function dataValidateLogin() | ||||
['test123456', 'unknown-domain.org', false, ['domain' => 'validation.domaininvalid']], | ['test123456', 'unknown-domain.org', false, ['domain' => 'validation.domaininvalid']], | ||||
['test.test', $domain, false, null], | ['test.test', $domain, false, null], | ||||
['test_test', $domain, false, null], | ['test_test', $domain, false, null], | ||||
['test-test', $domain, false, null], | ['test-test', $domain, false, null], | ||||
['admin', $domain, false, ['login' => 'validation.loginexists']], | ['admin', $domain, false, ['login' => 'validation.loginexists']], | ||||
['administrator', $domain, false, ['login' => 'validation.loginexists']], | ['administrator', $domain, false, ['login' => 'validation.loginexists']], | ||||
['sales', $domain, false, ['login' => 'validation.loginexists']], | ['sales', $domain, false, ['login' => 'validation.loginexists']], | ||||
['root', $domain, false, ['login' => 'validation.loginexists']], | ['root', $domain, false, ['login' => 'validation.loginexists']], | ||||
// existing user | // existing user | ||||
['SignupControllerTest1', $domain, false, ['login' => 'validation.loginexists']], | ['jack', 'kolab.org', true, ['domain' => 'validation.domainexists']], | ||||
// Domain account | // Domain account | ||||
['admin', 'kolabsys.com', true, null], | ['admin', 'kolabsys.com', true, null], | ||||
['testnonsystemdomain', 'invalid', true, ['domain' => 'validation.domaininvalid']], | ['testnonsystemdomain', 'invalid', true, ['domain' => 'validation.domaininvalid']], | ||||
['testnonsystemdomain', '.com', true, ['domain' => 'validation.domaininvalid']], | ['testnonsystemdomain', '.com', true, ['domain' => 'validation.domaininvalid']], | ||||
// existing user | // existing user | ||||
['SignupControllerTest1', $domain, true, ['domain' => 'validation.domainexists']], | ['john', 'kolab.org', true, ['domain' => 'validation.domainexists']], | ||||
]; | ]; | ||||
} | } | ||||
/** | /** | ||||
* Signup login/domain validation. | * Signup login/domain validation. | ||||
* | * | ||||
* Note: Technicly these include unit tests, but let's keep it here for now. | * Note: Technicly these include unit tests, but let's keep it here for now. | ||||
* FIXME: Shall we do a http request for each case? | * FIXME: Shall we do a http request for each case? | ||||
* | * | ||||
* @dataProvider dataValidateLogin | * @dataProvider dataValidateLogin | ||||
*/ | */ | ||||
public function testValidateLogin($login, $domain, $external, $expected_result) | public function testValidateLogin($login, $domain, $external, $expected_result) | ||||
{ | { | ||||
$method = new \ReflectionMethod('App\Http\Controllers\API\SignupController', 'validateLogin'); | $method = new \ReflectionMethod('App\Http\Controllers\API\SignupController', 'validateLogin'); | ||||
$method->setAccessible(true); | $method->setAccessible(true); | ||||
$result = $method->invoke(new SignupController(), $login, $domain, $external); | $result = $method->invoke(new SignupController(), $login, $domain, $external); | ||||
$this->assertSame($expected_result, $result); | $this->assertSame($expected_result, $result, var_export(func_get_args(), true)); | ||||
} | } | ||||
} | } |