Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Reseller/InvitationsTest.php
Show All 13 Lines | class InvitationsTest extends TestCase | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
SignupInvitation::truncate(); | SignupInvitation::truncate(); | ||||
\config(['app.tenant_id' => 1]); | |||||
self::useResellerUrl(); | self::useResellerUrl(); | ||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
SignupInvitation::truncate(); | SignupInvitation::truncate(); | ||||
\config(['app.tenant_id' => 1]); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test deleting invitations (DELETE /api/v4/invitations/<id>) | * Test deleting invitations (DELETE /api/v4/invitations/<id>) | ||||
*/ | */ | ||||
public function testDestroy(): void | public function testDestroy(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$reseller = $this->getTestUser('reseller@reseller.com'); | $reseller = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$reseller2 = $this->getTestUser('reseller@kolabnow.com'); | $reseller2 = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | |||||
\config(['app.tenant_id' => $tenant->id]); | |||||
$inv = SignupInvitation::create(['email' => 'email1@ext.com']); | $inv = SignupInvitation::create(['email' => 'email1@ext.com']); | ||||
$inv->tenant_id = $reseller->tenant_id; | |||||
$inv->save(); | |||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->delete("api/v4/invitations/{$inv->id}"); | $response = $this->actingAs($user)->delete("api/v4/invitations/{$inv->id}"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->delete("api/v4/invitations/{$inv->id}"); | $response = $this->actingAs($admin)->delete("api/v4/invitations/{$inv->id}"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Reseller user, but different tenant | // Reseller user, but different tenant | ||||
$response = $this->actingAs($reseller2)->delete("api/v4/invitations/{$inv->id}"); | $response = $this->actingAs($reseller2)->delete("api/v4/invitations/{$inv->id}"); | ||||
$response->assertStatus(403); | $response->assertStatus(404); | ||||
// Reseller - non-existing invitation identifier | // Reseller - non-existing invitation identifier | ||||
$response = $this->actingAs($reseller)->delete("api/v4/invitations/abd"); | $response = $this->actingAs($reseller)->delete("api/v4/invitations/abd"); | ||||
$response->assertStatus(404); | $response->assertStatus(404); | ||||
// Reseller - existing invitation | // Reseller - existing invitation | ||||
$response = $this->actingAs($reseller)->delete("api/v4/invitations/{$inv->id}"); | $response = $this->actingAs($reseller)->delete("api/v4/invitations/{$inv->id}"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
Show All 9 Lines | class InvitationsTest extends TestCase | ||||
* Test listing invitations (GET /api/v4/invitations) | * Test listing invitations (GET /api/v4/invitations) | ||||
*/ | */ | ||||
public function testIndex(): void | public function testIndex(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$reseller = $this->getTestUser('reseller@reseller.com'); | $reseller = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$reseller2 = $this->getTestUser('reseller@kolabnow.com'); | $reseller2 = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | $tenant = Tenant::where('title', 'Sample Tenant')->first(); | ||||
\config(['app.tenant_id' => $tenant->id]); | |||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->get("api/v4/invitations"); | $response = $this->actingAs($user)->get("api/v4/invitations"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->get("api/v4/invitations"); | $response = $this->actingAs($admin)->get("api/v4/invitations"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Reseller user, but different tenant | |||||
$response = $this->actingAs($reseller2)->get("api/v4/invitations"); | |||||
$response->assertStatus(403); | |||||
// Reseller (empty list) | // Reseller (empty list) | ||||
$response = $this->actingAs($reseller)->get("api/v4/invitations"); | $response = $this->actingAs($reseller)->get("api/v4/invitations"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(0, $json['count']); | $this->assertSame(0, $json['count']); | ||||
$this->assertSame([], $json['list']); | $this->assertSame([], $json['list']); | ||||
Show All 11 Lines | public function testIndex(): void | ||||
$i8 = SignupInvitation::create(['email' => 'email8@other.com']); | $i8 = SignupInvitation::create(['email' => 'email8@other.com']); | ||||
$i9 = SignupInvitation::create(['email' => 'email9@other.com']); | $i9 = SignupInvitation::create(['email' => 'email9@other.com']); | ||||
$i10 = SignupInvitation::create(['email' => 'email10@other.com']); | $i10 = SignupInvitation::create(['email' => 'email10@other.com']); | ||||
$i11 = SignupInvitation::create(['email' => 'email11@other.com']); | $i11 = SignupInvitation::create(['email' => 'email11@other.com']); | ||||
$i12 = SignupInvitation::create(['email' => 'email12@test.com']); | $i12 = SignupInvitation::create(['email' => 'email12@test.com']); | ||||
$i13 = SignupInvitation::create(['email' => 'email13@ext.com']); | $i13 = SignupInvitation::create(['email' => 'email13@ext.com']); | ||||
SignupInvitation::query()->update(['created_at' => now()->subDays('1')]); | SignupInvitation::query()->update(['created_at' => now()->subDays('1')]); | ||||
SignupInvitation::where('id', $i1->id) | SignupInvitation::where('id', $i1->id) | ||||
->update(['created_at' => now()->subHours('2'), 'status' => SignupInvitation::STATUS_FAILED]); | ->update(['created_at' => now()->subHours('2'), 'status' => SignupInvitation::STATUS_FAILED]); | ||||
SignupInvitation::where('id', $i2->id) | SignupInvitation::where('id', $i2->id) | ||||
->update(['created_at' => now()->subHours('3'), 'status' => SignupInvitation::STATUS_SENT]); | ->update(['created_at' => now()->subHours('3'), 'status' => SignupInvitation::STATUS_SENT]); | ||||
SignupInvitation::where('id', $i11->id)->update(['created_at' => now()->subDays('3')]); | SignupInvitation::where('id', $i11->id)->update(['created_at' => now()->subDays('3')]); | ||||
SignupInvitation::where('id', $i12->id)->update(['tenant_id' => 1]); | SignupInvitation::where('id', $i12->id)->update(['tenant_id' => $reseller2->tenant_id]); | ||||
SignupInvitation::where('id', $i13->id)->update(['tenant_id' => 1]); | SignupInvitation::where('id', $i13->id)->update(['tenant_id' => $reseller2->tenant_id]); | ||||
$response = $this->actingAs($reseller)->get("api/v4/invitations"); | $response = $this->actingAs($reseller)->get("api/v4/invitations"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(10, $json['count']); | $this->assertSame(10, $json['count']); | ||||
$this->assertSame(1, $json['page']); | $this->assertSame(1, $json['page']); | ||||
Show All 35 Lines | public function testIndex(): void | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(3, $json['count']); | $this->assertSame(3, $json['count']); | ||||
$this->assertSame(1, $json['page']); | $this->assertSame(1, $json['page']); | ||||
$this->assertFalse($json['hasMore']); | $this->assertFalse($json['hasMore']); | ||||
$this->assertSame($i1->id, $json['list'][0]['id']); | $this->assertSame($i1->id, $json['list'][0]['id']); | ||||
// Reseller user, but different tenant | |||||
$response = $this->actingAs($reseller2)->get("api/v4/invitations"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(2, $json['count']); | |||||
} | } | ||||
/** | /** | ||||
* Test resending invitations (POST /api/v4/invitations/<id>/resend) | * Test resending invitations (POST /api/v4/invitations/<id>/resend) | ||||
*/ | */ | ||||
public function testResend(): void | public function testResend(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$reseller = $this->getTestUser('reseller@reseller.com'); | $reseller = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$reseller2 = $this->getTestUser('reseller@kolabnow.com'); | $reseller2 = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | $tenant = Tenant::where('title', 'Sample Tenant')->first(); | ||||
\config(['app.tenant_id' => $tenant->id]); | |||||
$inv = SignupInvitation::create(['email' => 'email1@ext.com']); | $inv = SignupInvitation::create(['email' => 'email1@ext.com']); | ||||
$inv->tenant_id = $reseller->tenant_id; | |||||
$inv->save(); | |||||
SignupInvitation::where('id', $inv->id)->update(['status' => SignupInvitation::STATUS_FAILED]); | SignupInvitation::where('id', $inv->id)->update(['status' => SignupInvitation::STATUS_FAILED]); | ||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->post("api/v4/invitations/{$inv->id}/resend"); | $response = $this->actingAs($user)->post("api/v4/invitations/{$inv->id}/resend"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->post("api/v4/invitations/{$inv->id}/resend"); | $response = $this->actingAs($admin)->post("api/v4/invitations/{$inv->id}/resend"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Reseller user, but different tenant | // Reseller user, but different tenant | ||||
$response = $this->actingAs($reseller2)->post("api/v4/invitations/{$inv->id}/resend"); | $response = $this->actingAs($reseller2)->post("api/v4/invitations/{$inv->id}/resend"); | ||||
$response->assertStatus(403); | $response->assertStatus(404); | ||||
// Reseller - non-existing invitation identifier | // Reseller - non-existing invitation identifier | ||||
$response = $this->actingAs($reseller)->post("api/v4/invitations/abd/resend"); | $response = $this->actingAs($reseller)->post("api/v4/invitations/abd/resend"); | ||||
$response->assertStatus(404); | $response->assertStatus(404); | ||||
// Reseller - existing invitation | // Reseller - existing invitation | ||||
$response = $this->actingAs($reseller)->post("api/v4/invitations/{$inv->id}/resend"); | $response = $this->actingAs($reseller)->post("api/v4/invitations/{$inv->id}/resend"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
Show All 9 Lines | class InvitationsTest extends TestCase | ||||
* Test creating invitations (POST /api/v4/invitations) | * Test creating invitations (POST /api/v4/invitations) | ||||
*/ | */ | ||||
public function testStore(): void | public function testStore(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
$reseller = $this->getTestUser('reseller@reseller.com'); | $reseller = $this->getTestUser('reseller@sample-tenant.dev-local'); | ||||
$reseller2 = $this->getTestUser('reseller@kolabnow.com'); | $reseller2 = $this->getTestUser('reseller@' . \config('app.domain')); | ||||
$tenant = Tenant::where('title', 'Sample Tenant')->first(); | $tenant = Tenant::where('title', 'Sample Tenant')->first(); | ||||
\config(['app.tenant_id' => $tenant->id]); | |||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->post("api/v4/invitations", []); | $response = $this->actingAs($user)->post("api/v4/invitations", []); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->post("api/v4/invitations", []); | $response = $this->actingAs($admin)->post("api/v4/invitations", []); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Reseller user, but different tenant | |||||
$response = $this->actingAs($reseller2)->post("api/v4/invitations", []); | |||||
$response->assertStatus(403); | |||||
// Reseller (empty post) | // Reseller (empty post) | ||||
$response = $this->actingAs($reseller)->post("api/v4/invitations", []); | $response = $this->actingAs($reseller)->post("api/v4/invitations", []); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
Show All 17 Lines | public function testStore(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame("The invitation has been created.", $json['message']); | $this->assertSame("The invitation has been created.", $json['message']); | ||||
$this->assertSame(1, $json['count']); | $this->assertSame(1, $json['count']); | ||||
$this->assertSame(1, SignupInvitation::count()); | $this->assertSame(1, SignupInvitation::count()); | ||||
$invitation = SignupInvitation::first(); | |||||
$this->assertSame($reseller->tenant_id, $invitation->tenant_id); | |||||
$this->assertSame($post['email'], $invitation->email); | |||||
// Test file input (empty file) | // Test file input (empty file) | ||||
$tmpfile = tmpfile(); | $tmpfile = tmpfile(); | ||||
fwrite($tmpfile, ""); | fwrite($tmpfile, ""); | ||||
$file = new File('test.csv', $tmpfile); | $file = new File('test.csv', $tmpfile); | ||||
$post = ['file' => $file]; | $post = ['file' => $file]; | ||||
$response = $this->actingAs($reseller)->post("api/v4/invitations", $post); | $response = $this->actingAs($reseller)->post("api/v4/invitations", $post); | ||||
fclose($tmpfile); | fclose($tmpfile); | ||||
Show All 31 Lines | public function testStore(): void | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(1, SignupInvitation::where('email', 't1@domain.tld')->count()); | $this->assertSame(1, SignupInvitation::where('email', 't1@domain.tld')->count()); | ||||
$this->assertSame(1, SignupInvitation::where('email', 't2@domain.tld')->count()); | $this->assertSame(1, SignupInvitation::where('email', 't2@domain.tld')->count()); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame("2 invitations has been created.", $json['message']); | $this->assertSame("2 invitations has been created.", $json['message']); | ||||
$this->assertSame(2, $json['count']); | $this->assertSame(2, $json['count']); | ||||
// Reseller user, but different tenant | |||||
$post = ['email' => 'test-reseller2@external.org']; | |||||
$response = $this->actingAs($reseller2)->post("api/v4/invitations", $post); | |||||
$response->assertStatus(200); | |||||
$invitation = SignupInvitation::where('email', $post['email'])->first(); | |||||
$this->assertSame($reseller2->tenant_id, $invitation->tenant_id); | |||||
} | } | ||||
} | } |