ldc-crm/tests/CRM/ApiV2/QuestionTest.php

304 lines
11 KiB
PHP
Raw Permalink Normal View History

Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
<?php
namespace Tests\CRM\ApiV2;
use App\Enums\ApiError;
use App\Enums\ApiStatus;
use App\Models\CustomerAnswer;
use App\Models\Question;
use App\Models\Service;
class QuestionTest extends ApiBase
{
/**
* Test get all service questions.
* route: apiV2.questions.index
* @return void
*/
public function testQuestionIndex()
{
extract($this->getCustomer());
/** @var \Illuminate\Database\Eloquent\Collection $questions */
$questions = Question::whereHas('brand', function ($q) use ($service) {
$q->whereId($service->brand->id);
})->with('answers')->get()->keyBy('id');
$response = $this->callApiWithToken($service, 'get', route('apiV2.questions.index'));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$json->assertStructure([
'data' => [
'*' => [
'id',
'type',
'question',
'answers' => [
'*' => [
'id',
'answer'
]
],
]
]
]);
// Should contain questions with answers.
foreach ($json->json('data') as $question) {
$this->assertTrue($questions->contains($question['id']));
$DBquestion = $questions->get($question['id']);
$this->assertEquals($DBquestion->type->value, $question['type']);
$this->assertEquals($DBquestion->question, $question['question']);
foreach ($question['answers'] as $answer) {
$answers = $DBquestion->answers->keyBy('id');
$this->assertTrue($answers->contains($answer['id']));
$DBanswer = $answers->get($answer['id']);
$this->assertEquals($DBanswer->answer, $answer['answer']);
}
}
}
/**
* Test get all service questions with customer answers.
* route: apiV2.questions.indexWithResponse
* @return void
*/
public function testQuestionIndexWithAnswers()
{
extract($this->getCustomer(true, false));
/** @var \Illuminate\Database\Eloquent\Collection $questions */
$questions = Question::whereHas('brand', function ($q) use ($service) {
$q->whereId($service->brand->id);
})->with('answers')->get()->keyBy('id');
// * Make sure a customer answer is set.
$firstAnswer = $questions->first()->answers->first();
$firstAnswer->load('question');
/** @var \App\Models\CustomerAnswer $custAnswer */
$custAnswer = CustomerAnswer::whereHas('customer', function ($q) use ($customer) {
$q->whereId($customer->id);
})->whereHas('answer', function ($q) use ($firstAnswer) {
$q->whereId($firstAnswer->id);
})->firstOrNew();
$custAnswer->customer()->associate($customer);
$custAnswer->answer()->associate($firstAnswer);
$custAnswer->saveOrFail();
// * Call
$response = $this->callApiWithToken($service, 'get', route(
'apiV2.questions.indexWithResponse',
['serviceCustomer' => $info->hashcode]
));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$json->assertStructure([
'data' => [
'*' => [
'id',
'type',
'question',
'answers' => [
'*' => [
'id',
'answer'
]
],
'customerAnswers'
]
]
]);
// Should contain questions with answers.
foreach ($json->json('data') as $question) {
$this->assertTrue($questions->contains($question['id']));
$DBquestion = $questions->get($question['id']);
$this->assertEquals($DBquestion->type->value, $question['type']);
$this->assertEquals($DBquestion->question, $question['question']);
foreach ($question['answers'] as $answer) {
$answers = $DBquestion->answers->keyBy('id');
$this->assertTrue($answers->contains($answer['id']));
$DBanswer = $answers->get($answer['id']);
$this->assertEquals($DBanswer->answer, $answer['answer']);
}
if ($firstAnswer->question->id === $DBquestion->id) {
$this->assertEquals($question['customerAnswers'][0]['id'], $firstAnswer->id);
$this->assertEquals($question['customerAnswers'][0]['answer'], $firstAnswer->answer);
}
}
}
/**
* Test get all service questions with customer answers when customer is unknown.
* route: apiV2.questions.indexWithResponse
* @return void
*/
public function testQuestionIndexWithAnswersWhenCustomerNotFound()
{
extract($this->getCustomer(true, false));
$service = Service::whereHas('brand', function ($q) use ($brand) {
$q->where('id', '!=', $brand->id);
})->firstOrFail();
$response = $this->callApiWithToken($service, 'get', route(
'apiV2.questions.indexWithResponse',
['serviceCustomer' => $info->hashcode]
));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::modelNotFound()->value, $json->json('data.code'));
}
/**
* Test get all service questions with customer answers when customer is unknown.
* route: apiV2.questions.unAnsweredBy
* @return void
*/
public function testCustomerUnAnsweredIndex()
{
extract($this->getCustomer(true, false));
/** @var \Illuminate\Database\Eloquent\Collection $questions */
$questions = Question::whereHas('brand', function ($q) use ($service) {
$q->whereId($service->brand->id);
})->with('answers')->get()->keyBy('id');
// * Make sure a customer answer is set.
$firstAnswer = $questions->first()->answers->first();
$firstAnswer->load('question');
/** @var \App\Models\CustomerAnswer $custAnswer */
$custAnswer = CustomerAnswer::whereHas('customer', function ($q) use ($customer) {
$q->whereId($customer->id);
})->whereHas('answer', function ($q) use ($firstAnswer) {
$q->whereId($firstAnswer->id);
})->firstOrNew();
$custAnswer->customer()->associate($customer);
$custAnswer->answer()->associate($firstAnswer);
$custAnswer->saveOrFail();
// * Call
$response = $this->callApiWithToken($service, 'get', route(
'apiV2.questions.unAnsweredBy',
['serviceCustomer' => $info->hashcode]
));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$json->assertStructure([
'data' => [
'*' => [
'id',
'type',
'question',
'answers' => [
'*' => [
'id',
'answer'
]
]
]
]
]);
// Should contain questions with answers.
foreach ($json->json('data') as $question) {
$this->assertTrue($questions->contains($question['id']));
$DBquestion = $questions->get($question['id']);
$this->assertEquals($DBquestion->type->value, $question['type']);
$this->assertEquals($DBquestion->question, $question['question']);
foreach ($question['answers'] as $answer) {
$answers = $DBquestion->answers->keyBy('id');
$this->assertTrue($answers->contains($answer['id']));
$DBanswer = $answers->get($answer['id']);
$this->assertEquals($DBanswer->answer, $answer['answer']);
}
$this->assertNotEquals(
$firstAnswer->question->id,
$DBquestion->id,
'This question has been answered by our user'
);
}
}
/**
* Test get all service questions unanswered by customer when customer is unknown.
* route: apiV2.questions.indexWithResponse
* @return void
*/
public function testUnAnsweredIndexWhenCustomerNotFound()
{
extract($this->getCustomer(true, false));
$service = Service::whereHas('brand', function ($q) use ($brand) {
$q->where('id', '!=', $brand->id);
})->firstOrFail();
$response = $this->callApiWithToken($service, 'get', route(
'apiV2.questions.unAnsweredBy',
['serviceCustomer' => $info->hashcode]
));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::modelNotFound()->value, $json->json('data.code'));
}
/**
* Test save customer answer.
* route: apiV2.customers.answers.store
* @return void
*/
public function testQuestionSetAnswer()
{
extract($this->getCustomer(true, false));
$question = Question::whereHas('brand', function ($q) use ($brand) {
$q->whereId($brand->id);
})->whereHas('answers', function ($q) use ($customer) {
$q->whereDoesntHave('customers', function ($q) use ($customer) {
$q->whereId($customer->id);
});
})->inRandomOrder()->firstOrFail();
$answer = $question->answers->first();
$response = $this->callApiWithToken($service, 'post', route(
'apiV2.customers.answers.store',
['serviceCustomer' => $info->hashcode]
), [
'question' => $question->id,
'answers' => [$answer->id]
]);
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::OK());
$this->assertTrue(CustomerAnswer::whereHas('customer', function ($q) use ($customer) {
$q->whereId($customer->id);
})->whereHas('answer', function ($q) use ($answer) {
$q->whereId($answer->id);
})->exists(), 'Question should have been answered.');
}
/**
* Test get all save customer answers when customer is unknown.
* route: apiV2.questions.indexWithResponse
* @return void
*/
public function testQuestionSetAnswerWhenCustomerNotFound()
{
extract($this->getCustomer(true, false));
$service = Service::whereHas('brand', function ($q) use ($brand) {
$q->where('id', '!=', $brand->id);
})->firstOrFail();
$response = $this->callApiWithToken($service, 'post', route(
'apiV2.customers.answers.store',
['serviceCustomer' => $info->hashcode]
));
$json = $response->decodeResponseJson();
$this->assertApiResponseStatusIs($json, ApiStatus::KO());
$this->assertEquals(ApiError::modelNotFound()->value, $json->json('data.code'));
}
}