From e2ba3581f95b02d2601c0b2061154c8985d8719a Mon Sep 17 00:00:00 2001 From: shibafu Date: Fri, 24 Jul 2020 13:56:06 +0900 Subject: [PATCH] add test --- app/CheckinWebhook.php | 2 +- .../Controllers/Api/WebhookController.php | 4 +- database/factories/CheckinWebhookFactory.php | 12 +++ .../Api/Webhook/CheckinWebhookTest.php | 90 +++++++++++++++++++ tests/Feature/Setting/WebhookTest.php | 75 ++++++++++++++++ 5 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 database/factories/CheckinWebhookFactory.php create mode 100644 tests/Feature/Api/Webhook/CheckinWebhookTest.php create mode 100644 tests/Feature/Setting/WebhookTest.php diff --git a/app/CheckinWebhook.php b/app/CheckinWebhook.php index 9c8300b..1c7cde1 100644 --- a/app/CheckinWebhook.php +++ b/app/CheckinWebhook.php @@ -34,6 +34,6 @@ class CheckinWebhook extends Model public function isAvailable() { - return $this->user() !== null; + return $this->user !== null; } } diff --git a/app/Http/Controllers/Api/WebhookController.php b/app/Http/Controllers/Api/WebhookController.php index 6fff91a..75540c3 100644 --- a/app/Http/Controllers/Api/WebhookController.php +++ b/app/Http/Controllers/Api/WebhookController.php @@ -46,7 +46,7 @@ class WebhookController extends Controller 'message' => 'Validation failed', 'violations' => $validator->errors()->all(), ] - ]); + ], 422); } $ejaculatedDate = empty($inputs['checked_in_at']) ? now() : new Carbon($inputs['checked_in_at']); @@ -57,7 +57,7 @@ class WebhookController extends Controller 'error' => [ 'message' => 'Checkin already exists in this time', ] - ]); + ], 422); } $ejaculation = Ejaculation::create([ diff --git a/database/factories/CheckinWebhookFactory.php b/database/factories/CheckinWebhookFactory.php new file mode 100644 index 0000000..85e0879 --- /dev/null +++ b/database/factories/CheckinWebhookFactory.php @@ -0,0 +1,12 @@ +define(CheckinWebhook::class, function (Faker $faker) { + return [ + 'name' => 'example' + ]; +}); diff --git a/tests/Feature/Api/Webhook/CheckinWebhookTest.php b/tests/Feature/Api/Webhook/CheckinWebhookTest.php new file mode 100644 index 0000000..2afead6 --- /dev/null +++ b/tests/Feature/Api/Webhook/CheckinWebhookTest.php @@ -0,0 +1,90 @@ +seed(); + } + + public function testSuccessful() + { + $user = factory(User::class)->create(); + $webhook = factory(CheckinWebhook::class)->create(['user_id' => $user->id]); + + $response = $this->postJson('/api/webhooks/checkin/' . $webhook->id, [ + 'link' => 'http://example.com', + 'tags' => ['foo', 'bar'] + ]); + + $response->assertStatus(200) + ->assertJsonPath('status', 200); + + $checkinId = $response->json('checkin.id'); + $ejaculation = Ejaculation::find($checkinId); + $this->assertSame('http://example.com', $ejaculation->link); + $this->assertCount(2, $ejaculation->tags); + $this->assertSame(Ejaculation::SOURCE_WEBHOOK, $ejaculation->source); + $this->assertNotEmpty($ejaculation->checkin_webhook_id); + } + + public function testUserDestroyed() + { + $webhook = factory(CheckinWebhook::class)->create(['user_id' => null]); + + $response = $this->postJson('/api/webhooks/checkin/' . $webhook->id); + + $response->assertStatus(404) + ->assertJsonPath('status', 404) + ->assertJsonPath('error.message', 'The webhook is unavailable'); + } + + public function testValidationFailed() + { + $user = factory(User::class)->create(); + $webhook = factory(CheckinWebhook::class)->create(['user_id' => $user->id]); + + $response = $this->postJson('/api/webhooks/checkin/' . $webhook->id, [ + 'checked_in_at' => new Carbon('1999-12-31T23:59:00+0900'), + 'tags' => [ + 'Has spaces' + ] + ]); + + $response->assertStatus(422) + ->assertJsonPath('status', 422) + ->assertJsonPath('error.message', 'Validation failed') + ->assertJsonCount(2, 'error.violations'); + } + + public function testConflictCheckedInAt() + { + $user = factory(User::class)->create(); + $webhook = factory(CheckinWebhook::class)->create(['user_id' => $user->id]); + $ejaculatedDate = new Carbon('2020-07-21T19:19:00+0900'); + factory(Ejaculation::class)->create([ + 'user_id' => $user->id, + 'ejaculated_date' => $ejaculatedDate + ]); + + $response = $this->postJson('/api/webhooks/checkin/' . $webhook->id, [ + 'checked_in_at' => $ejaculatedDate, + ]); + + $response->assertStatus(422) + ->assertJsonPath('status', 422) + ->assertJsonPath('error.message', 'Checkin already exists in this time'); + } +} diff --git a/tests/Feature/Setting/WebhookTest.php b/tests/Feature/Setting/WebhookTest.php new file mode 100644 index 0000000..6db254b --- /dev/null +++ b/tests/Feature/Setting/WebhookTest.php @@ -0,0 +1,75 @@ +seed(); + } + + public function testStoreWebhooks() + { + $user = factory(User::class)->create(); + + $response = $this->actingAs($user) + ->followingRedirects() + ->post('/setting/webhooks', ['name' => 'example']); + + $response->assertStatus(200) + ->assertViewIs('setting.webhooks'); + $this->assertDatabaseHas('checkin_webhooks', ['user_id' => $user->id, 'name' => 'example']); + } + + public function testStoreWebhooksHas9Hooks() + { + $user = factory(User::class)->create(); + $webhooks = factory(CheckinWebhook::class, CheckinWebhook::PER_USER_LIMIT - 1)->create(['user_id' => $user->id]); + + $response = $this->actingAs($user) + ->followingRedirects() + ->post('/setting/webhooks', ['name' => 'example9']); + + $response->assertStatus(200) + ->assertViewIs('setting.webhooks'); + $this->assertDatabaseHas('checkin_webhooks', ['user_id' => $user->id, 'name' => 'example9']); + } + + public function testStoreWebhooksHas10Hooks() + { + $user = factory(User::class)->create(); + $webhooks = factory(CheckinWebhook::class, CheckinWebhook::PER_USER_LIMIT)->create(['user_id' => $user->id]); + + $response = $this->actingAs($user) + ->followingRedirects() + ->post('/setting/webhooks', ['name' => 'example10']); + + $response->assertStatus(200) + ->assertViewIs('setting.webhooks'); + $this->assertDatabaseMissing('checkin_webhooks', ['user_id' => $user->id, 'name' => 'example10']); + } + + public function testDestroyWebhooks() + { + $user = factory(User::class)->create(); + $webhook = factory(CheckinWebhook::class)->create(['user_id' => $user->id]); + + $response = $this->actingAs($user) + ->followingRedirects() + ->delete('/setting/webhooks/' . $webhook->id); + + $response->assertStatus(200) + ->assertViewIs('setting.webhooks') + ->assertSee('削除しました'); + $this->assertTrue($webhook->refresh()->trashed()); + } +}