Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/CompanionAppsTest.php
Show All 9 Lines | |||||
{ | { | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestUser('UsersControllerTest1@userscontroller.com'); | $this->deleteTestUser('CompanionAppsTest1@userscontroller.com'); | ||||
$this->deleteTestDomain('userscontroller.com'); | $this->deleteTestUser('CompanionAppsTest2@userscontroller.com'); | ||||
$this->deleteTestCompanionApp('testdevice'); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('UsersControllerTest1@userscontroller.com'); | $this->deleteTestUser('CompanionAppsTest1@userscontroller.com'); | ||||
$this->deleteTestDomain('userscontroller.com'); | $this->deleteTestUser('CompanionAppsTest2@userscontroller.com'); | ||||
$this->deleteTestCompanionApp('testdevice'); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test registering the app | * Test registering the app | ||||
*/ | */ | ||||
public function testRegister(): void | public function testRegister(): void | ||||
{ | { | ||||
$user = $this->getTestUser('CompanionAppsTest1@userscontroller.com'); | $user = $this->getTestUser('CompanionAppsTest1@userscontroller.com'); | ||||
$notificationToken = "notificationToken"; | $notificationToken = "notificationToken"; | ||||
$deviceId = "deviceId"; | $deviceId = "deviceId"; | ||||
$name = "testname"; | |||||
$response = $this->actingAs($user)->post( | $response = $this->actingAs($user)->post( | ||||
"api/v4/companion/register", | "api/v4/companion/register", | ||||
['notificationToken' => $notificationToken, 'deviceId' => $deviceId] | ['notificationToken' => $notificationToken, 'deviceId' => $deviceId, 'name' => $name] | ||||
); | ); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$companionApp = \App\CompanionApp::where('device_id', $deviceId)->first(); | $companionApp = \App\CompanionApp::where('device_id', $deviceId)->first(); | ||||
$this->assertTrue($companionApp != null); | $this->assertTrue($companionApp != null); | ||||
$this->assertEquals($deviceId, $companionApp->device_id); | $this->assertEquals($deviceId, $companionApp->device_id); | ||||
$this->assertEquals($name, $companionApp->name); | |||||
$this->assertEquals($notificationToken, $companionApp->notification_token); | $this->assertEquals($notificationToken, $companionApp->notification_token); | ||||
// Test a token update | // Test a token update | ||||
$notificationToken = "notificationToken2"; | $notificationToken = "notificationToken2"; | ||||
$response = $this->actingAs($user)->post( | $response = $this->actingAs($user)->post( | ||||
"api/v4/companion/register", | "api/v4/companion/register", | ||||
['notificationToken' => $notificationToken, 'deviceId' => $deviceId] | ['notificationToken' => $notificationToken, 'deviceId' => $deviceId, 'name' => $name] | ||||
); | ); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$companionApp->refresh(); | $companionApp->refresh(); | ||||
$this->assertEquals($notificationToken, $companionApp->notification_token); | $this->assertEquals($notificationToken, $companionApp->notification_token); | ||||
// Failing input valdiation | // Failing input valdiation | ||||
$response = $this->actingAs($user)->post( | $response = $this->actingAs($user)->post( | ||||
"api/v4/companion/register", | "api/v4/companion/register", | ||||
[] | [] | ||||
); | ); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
// Other users device | // Other users device | ||||
$user2 = $this->getTestUser('CompanionAppsTest2@userscontroller.com'); | $user2 = $this->getTestUser('CompanionAppsTest2@userscontroller.com'); | ||||
$response = $this->actingAs($user2)->post( | $response = $this->actingAs($user2)->post( | ||||
"api/v4/companion/register", | "api/v4/companion/register", | ||||
['notificationToken' => $notificationToken, 'deviceId' => $deviceId] | ['notificationToken' => $notificationToken, 'deviceId' => $deviceId, 'name' => $name] | ||||
); | ); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
} | } | ||||
public function testIndex(): void | |||||
{ | |||||
$response = $this->get("api/v4/companion"); | |||||
$response->assertStatus(401); | |||||
$user = $this->getTestUser('CompanionAppsTest1@userscontroller.com'); | |||||
machniak: This user should be deleted in setUp() and tearDown(). | |||||
$companionApp = $this->getTestCompanionApp( | |||||
'testdevice', | |||||
$user, | |||||
[ | |||||
'notification_token' => 'notificationtoken', | |||||
'mfa_enabled' => 1, | |||||
'name' => 'testname', | |||||
] | |||||
); | |||||
$response = $this->actingAs($user)->get("api/v4/companion"); | |||||
Done Inline ActionsThis user should be deleted in setUp() and tearDown(). machniak: This user should be deleted in setUp() and tearDown(). | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertCount(1, $json['list']); | |||||
$this->assertSame($user->id, $json['list'][0]['user_id']); | |||||
$this->assertSame($companionApp['device_id'], $json['list'][0]['device_id']); | |||||
$this->assertSame($companionApp['name'], $json['list'][0]['name']); | |||||
$this->assertSame($companionApp['notification_token'], $json['list'][0]['notification_token']); | |||||
$this->assertSame($companionApp['mfa_enabled'], $json['list'][0]['mfa_enabled']); | |||||
$user2 = $this->getTestUser('CompanionAppsTest2@userscontroller.com'); | |||||
$response = $this->actingAs($user2)->get( | |||||
"api/v4/companion" | |||||
); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame(0, $json['count']); | |||||
$this->assertCount(0, $json['list']); | |||||
Done Inline ActionsI miss a test for 403 case. machniak: I miss a test for 403 case. | |||||
} | |||||
public function testShow(): void | |||||
{ | |||||
$user = $this->getTestUser('CompanionAppsTest1@userscontroller.com'); | |||||
$companionApp = $this->getTestCompanionApp('testdevice', $user); | |||||
$response = $this->get("api/v4/companion/{$companionApp->id}"); | |||||
$response->assertStatus(401); | |||||
Done Inline ActionsJust testPairing. machniak: Just `testPairing`. | |||||
$response = $this->actingAs($user)->get("api/v4/companion/aaa"); | |||||
$response->assertStatus(404); | |||||
$response = $this->actingAs($user)->get("api/v4/companion/{$companionApp->id}"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame($companionApp->id, $json['id']); | |||||
$user2 = $this->getTestUser('CompanionAppsTest2@userscontroller.com'); | |||||
Done Inline ActionsWe should test that it is a data: URI. Maybe even also that it is a valid image content. machniak: We should test that it is a `data:` URI. Maybe even also that it is a valid image content. | |||||
$response = $this->actingAs($user2)->get("api/v4/companion/{$companionApp->id}"); | |||||
$response->assertStatus(403); | |||||
} | |||||
public function testPairing(): void | |||||
{ | |||||
$response = $this->get("api/v4/companion/pairing"); | |||||
$response->assertStatus(401); | |||||
$user = $this->getTestUser('CompanionAppsTest1@userscontroller.com'); | |||||
$response = $this->actingAs($user)->get("api/v4/companion/pairing"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertArrayHasKey('qrcode', $json); | |||||
$this->assertSame('data:image/svg+xml;base64,', substr($json['qrcode'], 0, 26)); | |||||
} | |||||
public function testDelete(): void | |||||
{ | |||||
$user = $this->getTestUser('CompanionAppsTest1@userscontroller.com'); | |||||
$companionApp = $this->getTestCompanionApp('testdevice', $user); | |||||
$response = $this->delete("api/v4/companion/{$companionApp->id}"); | |||||
$response->assertStatus(401); | |||||
$response = $this->actingAs($user)->delete("api/v4/companion/{$companionApp->id}"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertArrayHasKey('message', $json); | |||||
$companionApp = \App\CompanionApp::where('device_id', 'testdevice')->first(); | |||||
$this->assertTrue($companionApp == null); | |||||
} | |||||
} | } |
This user should be deleted in setUp() and tearDown().