Merge pull request 'Store answers atomically and limit word cloud input' (#31) from fix/atomic-votes into main
Reviewed-on: #31
This commit was merged in pull request #31.
This commit is contained in:
commit
6912d045cf
11 files changed
+224
-50
No files matched your search
@@ -5,6 +5,8 @@ namespace App\Test\TestCase\Controller;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Test\TestCase\PlenarioTestTrait;
|
||||
use Cake\Cache\Cache;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\ORM\TableRegistry;
|
||||
use Cake\TestSuite\IntegrationTestTrait;
|
||||
use Cake\TestSuite\TestCase;
|
||||
@@ -28,6 +30,7 @@ class ParticipateControllerTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
$this->enableCsrfToken();
|
||||
Cache::clear('throttle');
|
||||
}
|
||||
|
||||
public function testUnknownCodeIs404(): void
|
||||
@@ -157,4 +160,24 @@ class ParticipateControllerTest extends TestCase
|
||||
$this->get('/p/' . $poll->join_code);
|
||||
$this->assertResponseContains('Diese Umfrage ist geschlossen');
|
||||
}
|
||||
|
||||
public function testVotesAreThrottledPerIp(): void
|
||||
{
|
||||
Configure::write('Plenario.throttle.votesPerMinute', 2);
|
||||
[$poll] = $this->createPoll();
|
||||
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
|
||||
$this->goLive($poll, $slide);
|
||||
|
||||
foreach (['Kaffee', 'Tee', 'Wasser'] as $i => $word) {
|
||||
$this->cookie('plenario_pid', self::PID);
|
||||
$this->configRequest(['headers' => ['X-Requested-With' => 'XMLHttpRequest', 'Accept' => 'application/json']]);
|
||||
$this->post("/p/{$poll->join_code}/vote", ['slide_id' => $slide->id, 'word' => $word]);
|
||||
$body = json_decode((string)$this->_response->getBody(), true);
|
||||
$this->assertSame($i < 2, $body['ok'], $word);
|
||||
}
|
||||
$this->assertResponseCode(429);
|
||||
$this->assertStringContainsString('Zu viele Antworten', $body['message']);
|
||||
$this->assertArrayHasKey('state', $body);
|
||||
$this->assertSame(2, TableRegistry::getTableLocator()->get('Responses')->find()->where(['slide_id' => $slide->id])->count());
|
||||
}
|
||||
}
|
||||
@@ -210,4 +210,73 @@ class VoteServiceTest extends TestCase
|
||||
$this->expectExceptionMessage('Downvotes sind bei dieser Frage nicht möglich.');
|
||||
$service->submit($poll, $slide, 'p2', ['word' => 'Kaffee', 'vote' => 'down']);
|
||||
}
|
||||
|
||||
public function testWordcloudLimitsWordsPerRequest(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
|
||||
$poll = $this->goLive($poll, $slide);
|
||||
$words = array_map(fn(int $i) => 'Begriff ' . $i, range(1, VoteService::MAX_WORDS_PER_REQUEST + 1));
|
||||
|
||||
$this->expectExceptionMessage('höchstens ' . VoteService::MAX_WORDS_PER_REQUEST . ' Begriffe auf einmal');
|
||||
(new VoteService())->submit($poll, $slide, 'p1', ['words' => $words]);
|
||||
}
|
||||
|
||||
public function testWordcloudLimitsWordsPerParticipant(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
|
||||
$poll = $this->goLive($poll, $slide);
|
||||
$service = new VoteService();
|
||||
foreach (array_chunk(range(1, VoteService::MAX_WORDS), VoteService::MAX_WORDS_PER_REQUEST) as $chunk) {
|
||||
$service->submit($poll, $slide, 'p1', ['words' => array_map(fn(int $i) => 'Begriff ' . $i, $chunk)]);
|
||||
}
|
||||
$this->assertSame(VoteService::MAX_WORDS, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count());
|
||||
|
||||
// Others can still add words.
|
||||
$service->submit($poll, $slide, 'p2', ['word' => 'Noch einer']);
|
||||
|
||||
$this->expectExceptionMessage('höchstens ' . VoteService::MAX_WORDS . ' Begriffe');
|
||||
$service->submit($poll, $slide, 'p1', ['word' => 'Einer zu viel']);
|
||||
}
|
||||
|
||||
public function testArrayInputIsNeverStoredAsText(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$open = $this->createSlide($poll, Slide::TYPE_OPEN);
|
||||
$poll = $this->goLive($poll, $open);
|
||||
$service = new VoteService();
|
||||
try {
|
||||
$service->submit($poll, $open, 'p1', ['text' => ['injected']]);
|
||||
$this->fail('Array input must be rejected.');
|
||||
} catch (VoteException $e) {
|
||||
$this->assertSame('Bitte gib eine Antwort ein.', $e->getMessage());
|
||||
}
|
||||
|
||||
$cloud = $this->createSlide($poll, Slide::TYPE_WORDCLOUD);
|
||||
$poll = $this->goLive($poll, $cloud);
|
||||
$service->submit($poll, $cloud, 'p1', ['words' => [['nested'], 'Kaffee']]);
|
||||
$texts = $this->fetchTable('Responses')->find()->where(['slide_id' => $cloud->id])->all()->extract('text_value')->toList();
|
||||
$this->assertSame(['Kaffee'], $texts);
|
||||
}
|
||||
|
||||
public function testRejectedVoteLeavesNoRows(): void
|
||||
{
|
||||
[$poll] = $this->createPoll();
|
||||
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
|
||||
$poll = $this->goLive($poll, $slide);
|
||||
$service = new VoteService();
|
||||
$service->submit($poll, $slide, 'p1', ['option_id' => $slide->slide_options[0]->id]);
|
||||
try {
|
||||
// A repeated request (double click, retry) is rejected and stores nothing.
|
||||
$service->submit($poll, $slide, 'p1', ['option_id' => $slide->slide_options[1]->id]);
|
||||
$this->fail('Second vote must be rejected.');
|
||||
} catch (VoteException $e) {
|
||||
$this->assertSame('Du hast bereits abgestimmt.', $e->getMessage());
|
||||
}
|
||||
$this->assertSame(1, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count());
|
||||
// The lock does not touch the slide.
|
||||
$this->assertEquals($slide->modified, $this->slidesTable()->get($slide->id)->modified);
|
||||
$this->assertSame($slide->position, $this->slidesTable()->get($slide->id)->position);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user