|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use App\Models\Commit; |
| 6 | +use App\Models\Poll; |
| 7 | +use App\Models\PollVote; |
| 8 | +use App\Models\User; |
| 9 | + |
| 10 | +beforeEach(function (): void { |
| 11 | + $this->user = User::factory()->create(); |
| 12 | + $this->keyPair = apiKeyPairFor($this->user); |
| 13 | +}); |
| 14 | + |
| 15 | +it('votes and answers with the commit, whose poll now shows the results', function (): void { |
| 16 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 17 | + PollVote::factory()->for($poll->options[0], 'option')->count(2)->create(); |
| 18 | + |
| 19 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[1]->id]); |
| 20 | + |
| 21 | + $response->assertOk() |
| 22 | + ->assertJsonPath('data.id', $poll->commit_id) |
| 23 | + ->assertJsonPath('data.poll.has_voted', true) |
| 24 | + ->assertJsonPath('data.poll.total_votes', 3) |
| 25 | + ->assertJsonPath('data.poll.options.0.votes_count', 2) |
| 26 | + ->assertJsonPath('data.poll.options.0.voted', false) |
| 27 | + ->assertJsonPath('data.poll.options.0.percentage', 67) |
| 28 | + ->assertJsonPath('data.poll.options.1.votes_count', 1) |
| 29 | + ->assertJsonPath('data.poll.options.1.voted', true) |
| 30 | + ->assertJsonPath('data.poll.options.1.percentage', 33); |
| 31 | + |
| 32 | + expect($this->user->pollVotes()->sole()->poll_option_id)->toBe($poll->options[1]->id); |
| 33 | +}); |
| 34 | + |
| 35 | +it('answers with the commit as the commit endpoint shows it', function (): void { |
| 36 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 37 | + |
| 38 | + $response = signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]); |
| 39 | + |
| 40 | + $response->assertOk() |
| 41 | + ->assertExactJson(['data' => signedApiRequest($this->keyPair, 'GET', route('api-commit.show', $poll->commit))->json('data')]); |
| 42 | +}); |
| 43 | + |
| 44 | +it('requires an option of the poll', function (Closure $option, string $message): void { |
| 45 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 46 | + |
| 47 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $option()]) |
| 48 | + ->assertUnprocessable() |
| 49 | + ->assertJsonValidationErrors(['option' => $message]); |
| 50 | + |
| 51 | + expect(PollVote::query()->exists())->toBeFalse(); |
| 52 | +})->with([ |
| 53 | + 'missing' => [fn (): null => null, 'The option field is required.'], |
| 54 | + 'not a uuid' => [fn (): string => 'tabs', 'The option field must be a valid UUID.'], |
| 55 | + 'of another poll' => [fn (): string => Poll::factory()->withOptions('Vim', 'Emacs')->create()->options[0]->id, 'The selected option is invalid.'], |
| 56 | +]); |
| 57 | + |
| 58 | +it('may not vote on a commit without a poll', function (): void { |
| 59 | + $commit = Commit::factory()->create(); |
| 60 | + |
| 61 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $commit), ['option' => Poll::factory()->withOptions('Vim', 'Emacs')->create()->options[0]->id]) |
| 62 | + ->assertUnprocessable() |
| 63 | + ->assertJsonValidationErrors(['option' => 'The selected option is invalid.']); |
| 64 | +}); |
| 65 | + |
| 66 | +it('explains why a vote is refused, as the web does', function (Closure $poll, string $message): void { |
| 67 | + $poll = $poll($this->user); |
| 68 | + |
| 69 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]) |
| 70 | + ->assertUnprocessable() |
| 71 | + ->assertJsonValidationErrors(['option' => $message]); |
| 72 | + |
| 73 | + expect(PollVote::query()->exists())->toBeFalse(); |
| 74 | +})->with([ |
| 75 | + 'own poll' => [fn (User $user): Poll => Poll::factory()->for(Commit::factory()->for($user))->withOptions('Tabs', 'Spaces')->create(), "You can't vote on your own poll."], |
| 76 | + 'own pending poll' => [fn (User $user): Poll => Poll::factory()->for(Commit::factory()->for($user)->pending())->withOptions('Tabs', 'Spaces')->unopened()->create(), 'This poll is not open for votes.'], |
| 77 | + 'unopened poll' => [fn (User $user): Poll => Poll::factory()->withOptions('Tabs', 'Spaces')->unopened()->create(), 'This poll is not open for votes.'], |
| 78 | + 'closed poll' => [fn (User $user): Poll => Poll::factory()->withOptions('Tabs', 'Spaces')->closed()->create(), 'This poll has closed.'], |
| 79 | +]); |
| 80 | + |
| 81 | +it('refuses a second vote, since votes are final', function (): void { |
| 82 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 83 | + PollVote::factory()->for($poll->options[0], 'option')->for($this->user)->create(); |
| 84 | + |
| 85 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[1]->id]) |
| 86 | + ->assertUnprocessable() |
| 87 | + ->assertJsonValidationErrors(['option' => 'You have already voted on this poll.']); |
| 88 | + |
| 89 | + expect($this->user->pollVotes()->sole()->poll_option_id)->toBe($poll->options[0]->id); |
| 90 | +}); |
| 91 | + |
| 92 | +it('may not vote on a poll the user may not see', function (string $state): void { |
| 93 | + $poll = Poll::factory()->for(Commit::factory()->{$state}())->withOptions('Tabs', 'Spaces')->create(); |
| 94 | + |
| 95 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]) |
| 96 | + ->assertNotFound() |
| 97 | + ->assertDontSee($poll->commit->body); |
| 98 | + |
| 99 | + expect(PollVote::query()->exists())->toBeFalse(); |
| 100 | +})->with(['pending', 'rejected']); |
| 101 | + |
| 102 | +it('may not vote across a block in either direction', function (bool $voterBlocks): void { |
| 103 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 104 | + $author = $poll->commit->user; |
| 105 | + |
| 106 | + $voterBlocks ? $this->user->blocking()->attach($author) : $author->blocking()->attach($this->user); |
| 107 | + |
| 108 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]) |
| 109 | + ->assertNotFound(); |
| 110 | + |
| 111 | + expect(PollVote::query()->exists())->toBeFalse(); |
| 112 | +})->with([true, false]); |
| 113 | + |
| 114 | +it('may still vote across a mute, as on the web', function (): void { |
| 115 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 116 | + $this->user->muting()->attach($poll->commit->user); |
| 117 | + |
| 118 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]) |
| 119 | + ->assertOk() |
| 120 | + ->assertJsonPath('data.poll.has_voted', true); |
| 121 | +}); |
| 122 | + |
| 123 | +it('may not vote on a deleted or unknown commit', function (): void { |
| 124 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 125 | + $poll->commit->delete(); |
| 126 | + |
| 127 | + signedApiRequest($this->keyPair, 'POST', route('api-commit-poll-vote.store', $poll->commit_id), ['option' => $poll->options[0]->id]) |
| 128 | + ->assertNotFound(); |
| 129 | + |
| 130 | + signedApiRequest($this->keyPair, 'POST', 'api/v1/commits/0199e3a4-0000-7000-8000-000000000000/poll/votes', ['option' => $poll->options[0]->id]) |
| 131 | + ->assertNotFound(); |
| 132 | +}); |
| 133 | + |
| 134 | +it('requires a signed request', function (): void { |
| 135 | + $poll = Poll::factory()->withOptions('Tabs', 'Spaces')->create(); |
| 136 | + |
| 137 | + $this->postJson(route('api-commit-poll-vote.store', $poll->commit), ['option' => $poll->options[0]->id]) |
| 138 | + ->assertUnauthorized(); |
| 139 | + |
| 140 | + expect(PollVote::query()->exists())->toBeFalse(); |
| 141 | +}); |
0 commit comments