Skip to content

Commit

Permalink
Fixed issue #16538: Can add 2 survey participantes with the same toke…
Browse files Browse the repository at this point in the history
…n with the RPC api

Encrypt and validation unit test
  • Loading branch information
gabrieljenik committed Aug 19, 2020
1 parent b11095a commit e17c817
Showing 1 changed file with 85 additions and 6 deletions.
91 changes: 85 additions & 6 deletions tests/unit/models/EncryptAttributesTest.php
Expand Up @@ -22,26 +22,59 @@ public static function setupBeforeClass()
}

/**
* Test token.
* Test token without validation.
*/
public function testToken()
public function testTokenWithoutValidation()
{
// Get our token.
$tokens = \TokenDynamic::model(self::$surveyId)->findAll();
$this->assertNotEmpty($tokens);
$this->assertCount(1, $tokens);
$token = $tokens[0];
$token->decrypt();

// Change lastname.
$token->lastname = 'last';
$token->encryptSave(false);

// Load token and decrypt.
$tokens = \TokenDynamic::model(self::$surveyId)->findAll();
$this->assertCount(1, $tokens);
$token = $tokens[0];
$token->decrypt();
$this->assertEquals('last', $token->lastname);
$this->assertEquals('foo@bar.com', $token->email);

// Test the omitting decrypt() works.
$tokens = \TokenDynamic::model(self::$surveyId)->findAll();
$this->assertCount(1, $tokens);
$token = $tokens[0];
$this->assertNotEquals('last', $token->lastname);
}

/**
* Test token with validation.
*/
public function testTokenWithValidation()
{
// Get our token.
$tokens = \TokenDynamic::model(self::$surveyId)->findAll();
$this->assertNotEmpty($tokens);
$this->assertCount(1, $tokens);
$token = $tokens[0];
$token->decrypt();

// Change lastname.
$token->lastname = 'last';
$token->encryptSave();
$token->encryptSave(true);

// Load token and decrypt.
$tokens = \TokenDynamic::model(self::$surveyId)->findAll();
$this->assertCount(1, $tokens);
$token = $tokens[0];
$token->decrypt();
$this->assertEquals('last', $token->lastname);
$this->assertEquals('foo@bar.com', $token->email);

// Test the omitting decrypt() works.
$tokens = \TokenDynamic::model(self::$surveyId)->findAll();
Expand All @@ -53,13 +86,50 @@ public function testToken()
/**
* Test response.
*/
public function testResponse()
public function testResponseWithoutValidation()
{
$responses = \Response::model(self::$surveyId)->findAll();
$this->assertCount(1, $responses);
$response = $responses[0];
$response->decrypt();

// Get questions.
$survey = \Survey::model()->findByPk(self::$surveyId);
$questionObjects = $survey->groups[0]->questions;
$questions = [];
foreach ($questionObjects as $q) {
$questions[$q->title] = $q;
}

$sgqa = self::$surveyId . 'X' . $survey->groups[0]->gid . 'X' . $questions['Q00']->qid;

// Change answer
$response->$sgqa = "New answer.";
$response->encryptSave(false);

// Load answer
$responses = \Response::model(self::$surveyId)->findAll();
$this->assertCount(1, $responses);
$response = $responses[0];

$answer = $response->$sgqa;
$response->decrypt();
$decryptedAnswer = $response->$sgqa;

$this->assertEquals('New answer.', $decryptedAnswer);
$this->assertNotEquals('New answer.', $answer);
}

/**
* Test response.
*/
public function testResponseWithValidation()
{
$responses = \Response::model(self::$surveyId)->findAll();
$this->assertCount(1, $responses);
$response = $responses[0];
$response->decrypt();

// Get questions.
$survey = \Survey::model()->findByPk(self::$surveyId);
$questionObjects = $survey->groups[0]->questions;
Expand All @@ -70,11 +140,20 @@ public function testResponse()

$sgqa = self::$surveyId . 'X' . $survey->groups[0]->gid . 'X' . $questions['Q00']->qid;

// Change answer
$response->$sgqa = "New answer.";
$response->encryptSave(true);

// Load answer
$responses = \Response::model(self::$surveyId)->findAll();
$this->assertCount(1, $responses);
$response = $responses[0];

$answer = $response->$sgqa;
$response->decrypt();
$decryptedAnswer = $response->$sgqa;

$this->assertEquals('One answer.', $decryptedAnswer);
$this->assertNotEquals('One answer.', $answer);
$this->assertEquals('New answer.', $decryptedAnswer);
$this->assertNotEquals('New answer.', $answer);
}
}

0 comments on commit e17c817

Please sign in to comment.