Skip to content

Commit 98f593f

Browse files
committed
Added a check & response for expired voucher in validation endpoint
1 parent 7efcce5 commit 98f593f

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

app/Http/Controllers/Api/V1/ApiVoucherValidationController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ public function store(): JsonResponse
145145
]
146146
);
147147

148+
if (!is_null($voucher->expires_at)) {
149+
if ($voucher->expires_at <= now()) {
150+
$this->responseCode = 409;
151+
$this->message = ApiResponse::RESPONSE_REDEMPTION_FAILED_VOUCHER_EXPIRED->value;
152+
153+
return $this->respond();
154+
}
155+
}
156+
148157
/**
149158
* At this point everything checks out, we can return the voucher details
150159
*/

tests/Feature/API/App/VoucherValidation/VoucherValidationPostTest.php

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature\API\App\VoucherValidation;
44

5+
use App\Enums\ApiResponse;
56
use App\Models\Voucher;
67
use App\Models\VoucherSet;
78
use App\Models\VoucherSetMerchantTeam;
@@ -17,7 +18,7 @@ class VoucherValidationPostTest extends BaseAPITestCase
1718
protected string $endPoint = '/voucher-validation';
1819

1920
#[Test]
20-
public function itFailsIfVoucherIdentifierIsWrong()
21+
public function itCanValidateVoucherUsingId()
2122
{
2223

2324
$this->user = $this->createUserWithTeam();
@@ -34,20 +35,29 @@ public function itFailsIfVoucherIdentifierIsWrong()
3435
]
3536
);
3637

38+
$voucher = Voucher::factory()->create(
39+
[
40+
'voucher_set_id' => $voucherSet->id,
41+
]
42+
);
43+
44+
$voucher->refresh();
45+
3746
$payload = [
3847
'type' => 'voucher_id',
39-
'value' => 'INCORRECT VALUE',
48+
'value' => $voucher->id,
4049
];
4150

42-
$data = json_encode($payload);
43-
4451
$response = $this->postJson($this->apiRoot . $this->endPoint, $payload);
52+
$response->assertOk();
4553

46-
$response->assertStatus(404);
54+
$responseObj = json_decode($response->getContent());
55+
56+
$this->assertEquals($voucher->id, $responseObj->data->id);
4757
}
4858

4959
#[Test]
50-
public function itCanValidateVoucherUsingId()
60+
public function itCanValidateVoucherUsingShortCode()
5161
{
5262

5363
$this->user = $this->createUserWithTeam();
@@ -56,7 +66,8 @@ public function itCanValidateVoucherUsingId()
5666
$this->user
5767
);
5868

59-
$voucherSet = VoucherSet::factory()->createQuietly();
69+
$voucherSet = VoucherSet::factory()->createQuietly();
70+
6071
$voucherSetMerchant = VoucherSetMerchantTeam::factory()->createQuietly(
6172
[
6273
'voucher_set_id' => $voucherSet->id,
@@ -73,20 +84,20 @@ public function itCanValidateVoucherUsingId()
7384
$voucher->refresh();
7485

7586
$payload = [
76-
'type' => 'voucher_id',
77-
'value' => $voucher->id,
87+
'type' => 'voucher_code',
88+
'value' => $voucher->voucher_short_code,
7889
];
7990

8091
$response = $this->postJson($this->apiRoot . $this->endPoint, $payload);
81-
$response->assertOk();
8292

8393
$responseObj = json_decode($response->getContent());
8494

95+
$response->assertOk();
8596
$this->assertEquals($voucher->id, $responseObj->data->id);
8697
}
8798

8899
#[Test]
89-
public function itCanValidateVoucherUsingShortCode()
100+
public function itHidesSensitiveFieldsFromValidationResponse()
90101
{
91102

92103
$this->user = $this->createUserWithTeam();
@@ -123,10 +134,12 @@ public function itCanValidateVoucherUsingShortCode()
123134

124135
$response->assertOk();
125136
$this->assertEquals($voucher->id, $responseObj->data->id);
137+
$this->assertObjectNotHasProperty(propertyName: 'created_by_team_id', object: $responseObj->data);
138+
$this->assertObjectNotHasProperty(propertyName: 'allocated_to_service_team_id', object: $responseObj->data);
126139
}
127140

128141
#[Test]
129-
public function itHidesSensitiveFieldsFromValidationResponse()
142+
public function itFailsIfVoucherIdentifierIsWrong()
130143
{
131144

132145
$this->user = $this->createUserWithTeam();
@@ -135,8 +148,37 @@ public function itHidesSensitiveFieldsFromValidationResponse()
135148
$this->user
136149
);
137150

138-
$voucherSet = VoucherSet::factory()->createQuietly();
151+
$voucherSet = VoucherSet::factory()->createQuietly();
152+
$voucherSetMerchant = VoucherSetMerchantTeam::factory()->createQuietly(
153+
[
154+
'voucher_set_id' => $voucherSet->id,
155+
'merchant_team_id' => $this->user->current_team_id,
156+
]
157+
);
158+
159+
$payload = [
160+
'type' => 'voucher_id',
161+
'value' => 'INCORRECT VALUE',
162+
];
163+
164+
$data = json_encode($payload);
165+
166+
$response = $this->postJson($this->apiRoot . $this->endPoint, $payload);
167+
168+
$response->assertStatus(404);
169+
}
139170

171+
#[Test]
172+
public function itFailsIfVoucherIsExpired()
173+
{
174+
175+
$this->user = $this->createUserWithTeam();
176+
177+
Sanctum::actingAs(
178+
$this->user
179+
);
180+
181+
$voucherSet = VoucherSet::factory()->createQuietly();
140182
$voucherSetMerchant = VoucherSetMerchantTeam::factory()->createQuietly(
141183
[
142184
'voucher_set_id' => $voucherSet->id,
@@ -147,24 +189,24 @@ public function itHidesSensitiveFieldsFromValidationResponse()
147189
$voucher = Voucher::factory()->create(
148190
[
149191
'voucher_set_id' => $voucherSet->id,
192+
'expires_at' => now()->subMonth(),
150193
]
151194
);
152195

153-
$voucher->refresh();
154-
155196
$payload = [
156-
'type' => 'voucher_code',
157-
'value' => $voucher->voucher_short_code,
197+
'type' => 'voucher_id',
198+
'value' => $voucher->id,
158199
];
159200

160201
$response = $this->postJson($this->apiRoot . $this->endPoint, $payload);
161202

162-
$responseObj = json_decode($response->getContent());
203+
$responseObject = json_decode($response->getContent());
163204

164-
$response->assertOk();
165-
$this->assertEquals($voucher->id, $responseObj->data->id);
166-
$this->assertObjectNotHasProperty(propertyName: 'created_by_team_id', object: $responseObj->data);
167-
$this->assertObjectNotHasProperty(propertyName: 'allocated_to_service_team_id', object: $responseObj->data);
205+
/**
206+
* Assert status code & message are expected
207+
*/
208+
$response->assertStatus(409);
209+
$this->assertEquals($responseObject->meta->message, ApiResponse::RESPONSE_REDEMPTION_FAILED_VOUCHER_EXPIRED->value);
168210
}
169211

170212
#[Test]

0 commit comments

Comments
 (0)