Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup tests #3085

Merged
merged 3 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"filp/whoops": "^2.1",
"fzaninotto/faker": "^1.6",
"graham-campbell/testbench-core": "^1.1",
"laravel/browser-kit-testing": "^2.0",
"mockery/mockery": "0.9.9",
"nikic/php-parser": "^3.0",
"phpunit/phpunit": "5.7.20",
Expand Down
49 changes: 1 addition & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CachetHQ\Cachet\Settings\Cache;
use CachetHQ\Cachet\Settings\Repository;
use Illuminate\Contracts\Console\Kernel;
use Laravel\BrowserKitTesting\TestCase;
use Illuminate\Foundation\Testing\TestCase;

/**
* This is the abstract test case class.
Expand Down
8 changes: 6 additions & 2 deletions tests/Api/AbstractApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ abstract class AbstractApiTestCase extends AbstractTestCase
/**
* Become a user.
*
* @return void
* @return $this
*/
protected function beUser()
{
$this->user = factory(User::class)->create();
$this->user = factory(User::class)->create([
'username' => 'cachet-test',
]);

$this->be($this->user);

return $this;
}
}
134 changes: 85 additions & 49 deletions tests/Api/ComponentGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,109 +25,145 @@ class ComponentGroupTest extends AbstractApiTestCase
const COMPONENT_GROUP_1_NAME = 'Component Group 1';
const COMPONENT_GROUP_2_NAME = 'Component Group 2';

public function testGetGroups()
public function test_can_get_all_component_groups()
{
$groups = factory('CachetHQ\Cachet\Models\ComponentGroup', 3)
$groups = factory(ComponentGroup::class, 2)
->create(['visible' => ComponentGroup::VISIBLE_GUEST]);

$this->get('/api/v1/components/groups');
$this->seeJsonContains(['id' => $groups[0]->id]);
$this->seeJsonContains(['id' => $groups[1]->id]);
$this->seeJsonContains(['id' => $groups[2]->id]);
$this->assertResponseOk();
$response = $this->json('GET', '/api/v1/components/groups');

$response->assertStatus(200);
$response->assertJsonFragment([
[
'id' => $groups[0]->id,
'name' => $groups[0]->name,
'created_at' => (string) $groups[0]->created_at,
'updated_at' => (string) $groups[0]->updated_at,
'order' => $groups[0]->order,
'collapsed' => $groups[0]->collapsed,
'visible' => $groups[0]->visible,
'enabled_components' => $groups[0]->enabled_components,
'enabled_components_lowest' => $groups[0]->enabled_components_lowest,
'lowest_human_status' => $groups[0]->lowest_human_status,
],
]);
$response->assertJsonFragment([
[
'id' => $groups[1]->id,
'name' => $groups[1]->name,
'created_at' => (string) $groups[1]->created_at,
'updated_at' => (string) $groups[1]->updated_at,
'order' => $groups[1]->order,
'collapsed' => $groups[1]->collapsed,
'visible' => $groups[1]->visible,
'enabled_components' => $groups[1]->enabled_components,
'enabled_components_lowest' => $groups[1]->enabled_components_lowest,
'lowest_human_status' => $groups[1]->lowest_human_status,
],
]);
}

public function testGetInvalidGroup()
public function test_cannot_get_invalid_component_group()
{
$this->get('/api/v1/components/groups/1');
$this->assertResponseStatus(404);
$response = $this->json('GET', '/api/v1/components/groups/1');

$response->assertStatus(404);
}

public function testPostGroupUnauthorized()
public function test_cannot_create_component_group_without_authorization()
{
$this->post('/api/v1/components/groups');
$response = $this->json('POST', '/api/v1/components/groups');

$this->assertResponseStatus(401);
$response->assertStatus(401);
}

public function testPostGroupNoData()
public function test_cannot_create_component_group_without_data()
{
$this->beUser();

$this->post('/api/v1/components/groups');
$this->assertResponseStatus(400);
$response = $this->json('POST', '/api/v1/components/groups');

$response->assertStatus(400);
}

public function testPostGroup()
public function test_can_create_new_component_group()
{
$this->beUser();

$this->post('/api/v1/components/groups', [
$response = $this->json('POST', '/api/v1/components/groups', [
'name' => 'Foo',
'order' => 1,
'collapsed' => 1,
'visible' => ComponentGroup::VISIBLE_GUEST,
]);

$response->assertStatus(200);
$response->assertJsonFragment([
'name' => 'Foo',
'order' => 1,
'collapsed' => 1,
'visible' => ComponentGroup::VISIBLE_GUEST,
]);
$this->seeJsonContains(['name' => 'Foo', 'order' => 1, 'collapsed' => 1, 'visible' => ComponentGroup::VISIBLE_GUEST]);
$this->assertResponseOk();
}

public function testGetNewGroup()
public function test_can_get_single_component_group()
{
$group = factory('CachetHQ\Cachet\Models\ComponentGroup')->create();
$group = factory(ComponentGroup::class)->create();

$this->get('/api/v1/components/groups/1');
$this->seeJsonContains(['name' => $group->name]);
$this->assertResponseOk();
$response = $this->json('GET', '/api/v1/components/groups/1');

$response->assertStatus(200);
$response->assertJsonFragment(['name' => $group->name]);
}

public function testPutGroup()
public function test_can_update_component_group()
{
$this->beUser();
$group = factory('CachetHQ\Cachet\Models\ComponentGroup')->create();
$group = factory(ComponentGroup::class)->create();

$this->put('/api/v1/components/groups/1', [
$response = $this->json('PUT', '/api/v1/components/groups/1', [
'name' => 'Lorem Ipsum Groupous',
]);
$this->seeJsonContains(['name' => 'Lorem Ipsum Groupous']);
$this->assertResponseOk();

$response->assertStatus(200);
$response->assertJsonFragment(['name' => 'Lorem Ipsum Groupous']);
}

public function testDeleteGroup()
public function test_can_delete_component_group()
{
$this->beUser();
$group = factory('CachetHQ\Cachet\Models\ComponentGroup')->create();
$group = factory(ComponentGroup::class)->create();

$this->delete('/api/v1/components/groups/1');
$this->assertResponseStatus(204);
$response = $this->json('DELETE', '/api/v1/components/groups/1');

$response->assertStatus(204);
}

/** @test */
public function only_public_component_groups_are_shown_for_a_guest()
public function test_only_public_component_groups_are_shown_for_a_guest()
{
$this->createComponentGroups();

$this->get('/api/v1/components/groups')
->seeJsonContains(['name' => self::COMPONENT_GROUP_1_NAME]);
$this->assertResponseOk();
$response = $this->json('GET', '/api/v1/components/groups');

$response->assertStatus(200);
$response->assertJsonFragment(['name' => self::COMPONENT_GROUP_1_NAME]);
}

/** @test */
public function all_component_groups_are_displayed_for_loggedin_users()
public function test_all_component_groups_are_displayed_for_logged_in_users()
{
$this->createComponentGroups()
->signIn();

$this->get('/api/v1/components/groups')
->seeJsonContains(['name' => self::COMPONENT_GROUP_1_NAME])
->seeJsonContains(['name' => self::COMPONENT_GROUP_2_NAME]);
$this->assertResponseOk();
$response = $this->json('GET', '/api/v1/components/groups');

$response->assertStatus(200);
$response->assertJsonFragment(['name' => self::COMPONENT_GROUP_1_NAME]);
}

/**
* Set up the needed data for the tests.
*
* @return TestCase
* @return $this
*/
protected function createComponentGroups()
{
Expand All @@ -139,13 +175,13 @@ protected function createComponentGroups()

/**
* Create a component group.
* Also attaches a creator if any given as a parameter
* or exists in the test class.
*
* Also attaches a creator if any given as a parameter or exists in the test class.
*
* @param string $name
* @param string $visible
*
* @return AbstractApiTestCase
* @return $this
*/
protected function createComponentGroup($name, $visible)
{
Expand Down
Loading