forked from trypostit/trypost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostTemplateControllerTest.php
More file actions
175 lines (142 loc) · 5.94 KB
/
Copy pathPostTemplateControllerTest.php
File metadata and controls
175 lines (142 loc) · 5.94 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
use App\Enums\UserWorkspace\Role;
use App\Models\Account;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
uses(RefreshDatabase::class);
beforeEach(function () {
Storage::fake();
$this->account = Account::factory()->create();
$this->user = User::factory()->create([
'account_id' => $this->account->id,
]);
$this->account->update(['owner_id' => $this->user->id]);
$this->workspace = Workspace::factory()->create([
'account_id' => $this->account->id,
'user_id' => $this->user->id,
]);
$this->workspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
$this->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
});
test('index returns templates from the registry', function () {
$this->actingAs($this->user)
->get(route('app.post-templates.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('posts/templates/Index')
->has('templates.data')
);
});
test('index requires authentication', function () {
$this->get(route('app.post-templates.index'))
->assertRedirect(route('login'));
});
test('index filters by platform', function () {
$this->actingAs($this->user)
->get(route('app.post-templates.index', ['platform' => 'instagram_carousel']))
->assertOk()
->assertInertia(fn ($page) => $page
->component('posts/templates/Index')
->where('templates.data', fn ($templates) => collect($templates)->every(
fn ($t) => $t['platform'] === 'instagram_carousel'
))
);
});
test('apply creates post and returns post_id and redirect_url', function () {
Http::fake(['api.unsplash.com/*' => Http::response(['results' => []])]);
$response = $this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'success_story'))
->assertOk();
expect($response->json('post_id'))->toBeString();
expect($response->json('redirect_url'))->toContain('edit');
});
test('apply interpolates brand_name in content', function () {
Http::fake(['api.unsplash.com/*' => Http::response(['results' => []])]);
$this->workspace->update(['name' => 'Acme Corp']);
// success_story template content references {{brand_name}}
$this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'success_story'))
->assertOk();
$this->assertDatabaseHas('posts', [
'workspace_id' => $this->workspace->id,
]);
$post = $this->workspace->posts()->latest()->first();
expect($post->content)->toContain('Acme Corp');
});
test('apply rejects cross-workspace social_account_id', function () {
$otherAccount = Account::factory()->create();
$otherUser = User::factory()->create(['account_id' => $otherAccount->id]);
$otherAccount->update(['owner_id' => $otherUser->id]);
$otherWorkspace = Workspace::factory()->create([
'account_id' => $otherAccount->id,
'user_id' => $otherUser->id,
]);
$foreignSocialAccount = SocialAccount::factory()->create([
'workspace_id' => $otherWorkspace->id,
]);
$this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'success_story'), [
'social_account_id' => $foreignSocialAccount->id,
])
->assertForbidden();
});
test('apply requires authentication', function () {
$this->postJson(route('app.post-templates.apply', 'success_story'))
->assertUnauthorized();
});
test('apply returns 404 for unknown slug', function () {
$this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'this-slug-does-not-exist'))
->assertNotFound();
});
test('apply defaults scheduled_at to today when no date is provided', function () {
Http::fake(['api.unsplash.com/*' => Http::response(['results' => []])]);
$this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'success_story'))
->assertOk();
$post = $this->workspace->posts()->latest()->first();
expect($post->scheduled_at->format('Y-m-d'))->toBe(now('UTC')->format('Y-m-d'));
});
test('apply schedules the post on the date param when provided', function () {
Http::fake(['api.unsplash.com/*' => Http::response(['results' => []])]);
$this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'success_story'), [
'date' => '2026-06-15',
])
->assertOk();
$post = $this->workspace->posts()->latest()->first();
expect($post->scheduled_at->format('Y-m-d'))->toBe('2026-06-15');
});
test('apply rejects invalid date format', function () {
$this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'success_story'), [
'date' => 'not-a-date',
])
->assertUnprocessable()
->assertJsonValidationErrors(['date']);
});
test('apply with slides creates post even when image rendering fails', function () {
// Unsplash returns no results → generator returns null → no media attached, post still created.
Http::fake(['api.unsplash.com/*' => Http::response(['results' => []])]);
$socialAccount = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
]);
$response = $this->actingAs($this->user)
->postJson(route('app.post-templates.apply', 'feature_launch_carousel'), [
'social_account_id' => $socialAccount->id,
])
->assertOk();
expect($response->json('post_id'))->toBeString();
expect($response->json('redirect_url'))->toContain('edit');
});