Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vinzenz Rosenkranz <[email protected]>
  • Loading branch information
v1r0x committed Dec 9, 2020
1 parent 871a073 commit 1219abd
Showing 1 changed file with 108 additions and 2 deletions.
110 changes: 108 additions & 2 deletions tests/Feature/ApiUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ public function testPatchUserEndpoint()
'email' => '[email protected]',
'name' => 'Admin Updated',
'nickname' => 'admin1',
'phonenumber' => '+43 123 1234'
'phonenumber' => '+43 123 1234',
'orcid' => '0000-0002-1694-233X'
]);

$user = User::find(1);
Expand All @@ -405,17 +406,104 @@ public function testPatchUserEndpoint()
'avatar_url' => null,
'metadata' => [
'phonenumber' => '+43 123 1234',
'orcid' => '0000-0002-1694-233X',
],
]);

$this->assertEquals('Admin Updated', $user->name);
$this->assertEquals('admin1', $user->nickname);
$this->assertEquals('[email protected]', $user->email);
$this->assertEquals('+43 123 1234', $user->metadata['phonenumber']);
$this->assertEquals('0000-0002-1694-233X', $user->metadata['orcid']);
$this->assertNull($user->avatar);
$this->assertNull($user->avatar_url);
}

/**
* Test patching a users existing metadata.
*
* @return void
*/
public function testPatchUserOrcidEndpoint()
{
$user = User::find(1);
$this->assertNull($user->metadata);
$user->metadata = [
'orcid' => '0000-0002-1694-233X',
];
$user->save();

$response = $this->withHeaders([
'Authorization' => "Bearer $this->token"
])
->patch('/api/v1/user/1', [
'orcid' => '0000-0001-5109-3700'
]);

$response->assertStatus(200);
$user = User::find(1);
$this->assertNotNull($user->metadata);
$this->assertEquals('0000-0001-5109-3700', $user->metadata['orcid']);

}

/**
* Test patching user with wrong orcid
*
* @return void
*/
public function testPatchUserWrongOrcidEndpoint()
{
$user = User::find(1);

$response = $this->withHeaders([
'Authorization' => "Bearer $this->token"
])
->json('patch', '/api/v1/user/1', [
'orcid' => '0000-0002-1694-2338'
]);

$response->assertStatus(422);
}

/**
* Test patching user with wrong orcid
*
* @return void
*/
public function testPatchUserAnotherWrongOrcidEndpoint()
{
$user = User::find(1);

$response = $this->withHeaders([
'Authorization' => "Bearer $this->token"
])
->json('patch', '/api/v1/user/1', [
'orcid' => '0000-0002-1694233X'
]);

$response->assertStatus(422);
}

/**
* Test patching user with wrong orcid
*
* @return void
*/
public function testPatchUserAnotherSecondWrongOrcidEndpoint()
{
$user = User::find(1);

$response = $this->withHeaders([
'Authorization' => "Bearer $this->token"
])
->json('patch', '/api/v1/user/1', [
'orcid' => '0000-0002-1694-23aX'
]);

$response->assertStatus(422);
}

/**
* Test patching user with empty request.
*
Expand Down Expand Up @@ -488,7 +576,7 @@ public function testPatchRoleWithoutDataEndpoint()
}

/**
* Test deleting user (id=1).
* Test deleting and restoring a user (id=1).
*
* @return void
*/
Expand All @@ -513,6 +601,22 @@ public function testDeleteUserEndpoint()
$this->assertEquals(1, $cnt);
$cnt = User::withoutTrashed()->count();
$this->assertEquals(0, $cnt);
$user = User::find(1);
$this->assertNotNull($user->deleted_at);


// Test restore
$this->refreshToken($response);

$response = $this->withHeaders([
'Authorization' => "Bearer $this->token"
])
->patch('/api/v1/user/restore/1');

$user = User::find(1);
$this->assertNull($user->deleted_at);

$response->assertStatus(204);
}

/**
Expand Down Expand Up @@ -651,6 +755,7 @@ public function testPermissions()
['url' => '/role', 'error' => 'You do not have the permission to add roles', 'verb' => 'post'],
['url' => '/user/1', 'error' => 'You do not have the permission to set user roles', 'verb' => 'patch'],
['url' => '/role/1', 'error' => 'You do not have the permission to set role permissions', 'verb' => 'patch'],
['url' => '/user/restore/1', 'error' => 'You do not have the permission to delete users', 'verb' => 'patch'],
['url' => '/user/1', 'error' => 'You do not have the permission to delete users', 'verb' => 'delete'],
['url' => '/role/1', 'error' => 'You do not have the permission to delete roles', 'verb' => 'delete'],
['url' => '/user/1/avatar', 'error' => 'You do not have the permission to delete users', 'verb' => 'delete'],
Expand Down Expand Up @@ -681,6 +786,7 @@ public function testExceptions()
$calls = [
['url' => '/user/99', 'error' => 'This user does not exist', 'verb' => 'patch'],
['url' => '/role/99', 'error' => 'This role does not exist', 'verb' => 'patch'],
['url' => '/user/restore/99', 'error' => 'This user does not exist', 'verb' => 'patch'],
];

foreach($calls as $c) {
Expand Down

0 comments on commit 1219abd

Please sign in to comment.