-
-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathEmailTemplateTokenTest.php
More file actions
159 lines (127 loc) · 5.29 KB
/
Copy pathEmailTemplateTokenTest.php
File metadata and controls
159 lines (127 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Tests\Feature\Http\Actions\EmailTemplates;
use HiEvents\Http\ResponseCodes;
use HiEvents\Models\Account;
use HiEvents\Models\AccountConfiguration;
use HiEvents\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class EmailTemplateTokenTest extends TestCase
{
use DatabaseTransactions;
private User $user;
private Account $account;
private string $authToken;
protected function setUp(): void
{
parent::setUp();
// Create account configuration (required by auth system)
AccountConfiguration::firstOrCreate(['id' => 1], [
'id' => 1,
'name' => 'Default',
'is_system_default' => true,
'application_fees' => [
'percentage' => 1.5,
'fixed' => 0,
]
]);
// Create user with account
$password = 'password123';
$this->user = User::factory()->password($password)->withAccount()->create();
// Get the account created by withAccount()
$this->account = $this->user->accounts()->first();
// Login to get JWT token
$loginResponse = $this->postJson('/auth/login', [
'email' => $this->user->email,
'password' => $password,
]);
$this->authToken = $loginResponse->headers->get('X-Auth-Token');
}
public function test_can_get_order_confirmation_tokens(): void
{
$response = $this->getJson('/email-templates/tokens/order_confirmation', [
'Authorization' => 'Bearer ' . $this->authToken,
]);
$response->assertStatus(ResponseCodes::HTTP_OK)
->assertJsonStructure([
'tokens' => [
'*' => [
'token',
'description',
'example',
],
],
]);
$tokens = $response->json('tokens');
$this->assertNotEmpty($tokens);
$firstNameToken = collect($tokens)->firstWhere('token', '{{ order.first_name }}');
$this->assertNotNull($firstNameToken);
$this->assertEquals('The first name of the person who placed the order', $firstNameToken['description']);
$lastNameToken = collect($tokens)->firstWhere('token', '{{ order.last_name }}');
$this->assertNotNull($lastNameToken);
$this->assertEquals('The last name of the person who placed the order', $lastNameToken['description']);
}
public function test_can_get_attendee_ticket_tokens(): void
{
$response = $this->getJson('/email-templates/tokens/attendee_ticket', [
'Authorization' => 'Bearer ' . $this->authToken,
]);
$response->assertStatus(ResponseCodes::HTTP_OK)
->assertJsonStructure([
'tokens' => [
'*' => [
'token',
'description',
'example',
],
],
]);
$tokens = $response->json('tokens');
$this->assertNotEmpty($tokens);
$attendeeNameToken = collect($tokens)->firstWhere('token', '{{ attendee.name }}');
$this->assertNotNull($attendeeNameToken);
}
public function test_invalid_template_type_returns_validation_error(): void
{
$response = $this->getJson('/email-templates/tokens/invalid_type', [
'Authorization' => 'Bearer ' . $this->authToken,
]);
$response->assertStatus(ResponseCodes::HTTP_BAD_REQUEST);
}
public function test_unauthenticated_user_cannot_access_tokens(): void
{
$response = $this->getJson('/email-templates/tokens/order_confirmation');
$response->assertStatus(ResponseCodes::HTTP_INTERNAL_SERVER_ERROR);
}
public function test_tokens_include_order_specific_tokens(): void
{
$response = $this->getJson('/email-templates/tokens/order_confirmation', [
'Authorization' => 'Bearer ' . $this->authToken,
]);
$response->assertStatus(ResponseCodes::HTTP_OK);
$tokens = $response->json('tokens');
$tokenNames = collect($tokens)->pluck('token')->toArray();
$this->assertContains('{{ event.title }}', $tokenNames);
$this->assertContains('{{ order.number }}', $tokenNames);
$this->assertContains('{{ order.total }}', $tokenNames);
$this->assertContains('{{ organizer.name }}', $tokenNames);
}
public function test_tokens_have_proper_structure(): void
{
$response = $this->getJson('/email-templates/tokens/order_confirmation', [
'Authorization' => 'Bearer ' . $this->authToken,
]);
$response->assertStatus(ResponseCodes::HTTP_OK);
$tokens = $response->json('tokens');
foreach ($tokens as $token) {
$this->assertArrayHasKey('token', $token);
$this->assertArrayHasKey('description', $token);
$this->assertArrayHasKey('example', $token);
$this->assertNotEmpty($token['description']);
// Most tokens should start with {{ and end with }}
if (str_starts_with($token['token'], '{{')) {
$this->assertStringEndsWith('}}', $token['token']);
}
}
}
}