|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Domains\Discussions\Tests\Feature; |
| 4 | + |
| 5 | +use Domains\Accounts\Database\Factories\UserFactory; |
| 6 | +use Domains\Accounts\Models\User; |
| 7 | +use Domains\Discussions\Database\Factories\QuestionFactory; |
| 8 | +use Domains\Discussions\Models\Question; |
| 9 | +use Illuminate\Http\Response; |
| 10 | +use Laravel\Lumen\Testing\DatabaseMigrations; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class QuestionsViewTest extends TestCase |
| 14 | +{ |
| 15 | + use DatabaseMigrations; |
| 16 | + |
| 17 | + private User $user; |
| 18 | + private Question $question; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + $this->user = UserFactory::new()->create(); |
| 25 | + $this->question = QuestionFactory::new(['author_id' => $this->user->id])->create(); |
| 26 | + } |
| 27 | + |
| 28 | + /** @test */ |
| 29 | + public function it_allow_guest_see_a_question(): void |
| 30 | + { |
| 31 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])) |
| 32 | + ->seeJson([ |
| 33 | + 'data' => [ |
| 34 | + 'id' => $this->question->id, |
| 35 | + 'title' => $this->question->title, |
| 36 | + 'slug' => $this->question->slug, |
| 37 | + 'description' => $this->question->description, |
| 38 | + 'author' => [ |
| 39 | + 'id' => $this->user->id, |
| 40 | + 'name' => $this->user->name, |
| 41 | + 'email' => $this->user->email, |
| 42 | + 'trusted' => $this->user->trusted ? "1" : "0", |
| 43 | + 'created_at' => $this->user->created_at, |
| 44 | + 'updated_at' => $this->user->updated_at, |
| 45 | + 'deleted_at' => $this->user->deleted_at |
| 46 | + ], |
| 47 | + 'created_at' => $this->question->created_at, |
| 48 | + 'updated_at' => $this->question->updated_at, |
| 49 | + 'deleted_at' => $this->question->deleted_at |
| 50 | + ] |
| 51 | + ]); |
| 52 | + } |
| 53 | + |
| 54 | + /** @test */ |
| 55 | + public function it_allow_authenticated_user_see_a_question(): void |
| 56 | + { |
| 57 | + $this->actingAs($this->user); |
| 58 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])) |
| 59 | + ->seeJsonStructure([ |
| 60 | + 'data' => [ |
| 61 | + 'id', |
| 62 | + 'title', |
| 63 | + 'slug', |
| 64 | + 'description', |
| 65 | + 'author', |
| 66 | + 'created_at', |
| 67 | + 'updated_at', |
| 68 | + 'deleted_at' |
| 69 | + ] |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + /** @test */ |
| 74 | + public function it_blocked_guest_for_many_attempts(): void |
| 75 | + { |
| 76 | + for ($attempt = 0; $attempt < 30; ++$attempt) { |
| 77 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])) |
| 78 | + ->assertResponseStatus(Response::HTTP_OK); |
| 79 | + } |
| 80 | + |
| 81 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])) |
| 82 | + ->assertResponseStatus(Response::HTTP_TOO_MANY_REQUESTS); |
| 83 | + } |
| 84 | + |
| 85 | + /** @test */ |
| 86 | + public function it_not_blocked_authenticated_user_for_many_attempts(): void |
| 87 | + { |
| 88 | + $this->actingAs($this->user); |
| 89 | + |
| 90 | + for ($attempt = 0; $attempt < 30; ++$attempt) { |
| 91 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])); |
| 92 | + } |
| 93 | + |
| 94 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])) |
| 95 | + ->assertResponseStatus(Response::HTTP_OK); |
| 96 | + } |
| 97 | + |
| 98 | + /** @test */ |
| 99 | + public function it_fails_on_invalid_question(): void |
| 100 | + { |
| 101 | + $this->get(route('discussions.questions.view', ['questionId' => 1000])) |
| 102 | + ->assertResponseStatus(Response::HTTP_NOT_FOUND); |
| 103 | + } |
| 104 | + |
| 105 | + /** @test */ |
| 106 | + public function it_fails_on_view_a_deleted_question(): void |
| 107 | + { |
| 108 | + $this->question->delete(); |
| 109 | + $this->get(route('discussions.questions.view', ['questionId' => $this->question->id])) |
| 110 | + ->assertResponseStatus(Response::HTTP_NOT_FOUND); |
| 111 | + } |
| 112 | +} |
0 commit comments