File size: 10,391 Bytes
6773345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

namespace Tests\Unit;

use App\Domain\Identity\ClientOnboardingService;
use App\Shared\Exceptions\ApiException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PDO;
use Tests\TestCase;

class ClientOnboardingSetupTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        if (! in_array('sqlite', PDO::getAvailableDrivers(), true)) {
            $this->markTestSkipped('SQLite PDO driver is required for client onboarding database regression tests.');
        }

        foreach ([
            'communication_delivery_attempts',
            'communication_messages',
            'audit_events',
            'user_roles',
            'roles',
            'users',
            'client_account_invitations',
            'contracts',
            'payments',
            'invoices',
            'applications',
        ] as $table) {
            Schema::dropIfExists($table);
        }

        $this->createSchema();
        $this->seedEligibleApplication();
    }

    public function test_contract_acceptance_invitation_uses_client_setup_url_and_sends_email(): void
    {
        config(['app.client_portal_setup_url' => 'https://example.test']);

        $result = app(ClientOnboardingService::class)->inviteAfterContractAcceptance('app-1');

        $this->assertSame('INVITED', $result['status']);
        $this->assertSame('applicant@example.com', $result['email']);
        $this->assertTrue($result['email_sent']);
        $this->assertStringStartsWith('https://example.test/client/setup/', $result['setup_url']);
        $this->assertSame(1, DB::table('client_account_invitations')->where('status', 'INVITED')->count());

        $message = DB::table('communication_messages')->where('event_type', 'CLIENT_PORTAL_SETUP_INVITED')->first();
        $this->assertNotNull($message);
        $this->assertStringContainsString($result['setup_url'], $message->body);
        $this->assertStringNotContainsString('internal My Diaspora Nexus email address', $message->body);
    }

    public function test_setup_token_creates_client_account_and_cannot_be_reused(): void
    {
        config(['app.client_portal_setup_url' => 'https://example.test']);

        $invitation = app(ClientOnboardingService::class)->inviteAfterContractAcceptance('app-1');
        $token = basename(parse_url($invitation['setup_url'], PHP_URL_PATH));

        $setup = app(ClientOnboardingService::class)->getSetupInvitation($token);
        $this->assertSame('applicant@example.com', $setup['invitation']['email']);

        $result = app(ClientOnboardingService::class)->completeSetup($token, 'StrongPass123!');
        $this->assertSame('applicant@example.com', $result['user']['email']);
        $this->assertSame('CLIENT', $result['user']['type']);

        $storedInvitation = DB::table('client_account_invitations')->where('id', $invitation['invitation_id'])->first();
        $application = DB::table('applications')->where('id', 'app-1')->first();
        $this->assertSame('ACCEPTED', $storedInvitation->status);
        $this->assertNotNull($storedInvitation->user_id);
        $this->assertSame($storedInvitation->user_id, $application->user_id);

        $this->expectException(ApiException::class);
        app(ClientOnboardingService::class)->getSetupInvitation($token);
    }

    private function createSchema(): void
    {
        Schema::create('applications', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('application_reference')->nullable();
            $table->string('status');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('email')->nullable();
            $table->string('user_id')->nullable();
            $table->timestamp('approved_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('invoices', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('application_id')->nullable();
            $table->string('fee_type_code');
            $table->string('status');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('payments', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('invoice_id');
            $table->string('status');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('contracts', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('application_id')->nullable();
            $table->string('status');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('client_account_invitations', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('application_id');
            $table->string('user_id')->nullable();
            $table->string('email');
            $table->string('token_hash')->unique();
            $table->string('status');
            $table->timestamp('expires_at');
            $table->timestamp('accepted_at')->nullable();
            $table->string('invited_by_user_id')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('users', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('email');
            $table->string('email_normalized')->nullable();
            $table->string('password_hash')->nullable();
            $table->string('user_status')->nullable();
            $table->string('user_type')->nullable();
            $table->string('portal_status')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('roles', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('code');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('user_roles', function (Blueprint $table): void {
            $table->string('user_id');
            $table->string('role_id');
            $table->timestamp('effective_from')->nullable();
            $table->timestamp('effective_to')->nullable();
            $table->string('assigned_by_user_id')->nullable();
            $table->string('assignment_reason')->nullable();
        });

        Schema::create('audit_events', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('event_type');
            $table->string('event_category');
            $table->string('actor_user_id')->nullable();
            $table->string('actor_role_code')->nullable();
            $table->string('subject_type');
            $table->string('subject_id')->nullable();
            $table->string('affected_owner_user_id')->nullable();
            $table->string('result');
            $table->timestamp('occurred_at');
            $table->string('correlation_id')->nullable();
            $table->string('ip_address')->nullable();
            $table->text('user_agent')->nullable();
            $table->text('metadata')->nullable();
        });

        Schema::create('communication_messages', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('template_id')->nullable();
            $table->string('channel');
            $table->string('recipient_user_id')->nullable();
            $table->string('recipient_email')->nullable();
            $table->string('recipient_phone')->nullable();
            $table->string('event_type');
            $table->string('subject')->nullable();
            $table->text('body');
            $table->string('status');
            $table->timestamp('queued_at')->nullable();
            $table->timestamp('sent_at')->nullable();
            $table->string('correlation_id')->nullable();
            $table->text('context')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('communication_delivery_attempts', function (Blueprint $table): void {
            $table->string('id')->primary();
            $table->string('message_id');
            $table->integer('attempt_number');
            $table->string('provider_code');
            $table->string('provider_reference')->nullable();
            $table->string('status');
            $table->timestamp('attempted_at');
            $table->string('error_code')->nullable();
            $table->text('error_message')->nullable();
            $table->timestamps();
        });
    }

    private function seedEligibleApplication(): void
    {
        DB::table('applications')->insert([
            'id' => 'app-1',
            'application_reference' => 'APP-1',
            'status' => 'APPROVED',
            'first_name' => 'Test',
            'last_name' => 'Applicant',
            'email' => 'applicant@example.com',
            'approved_at' => now(),
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('invoices')->insert([
            'id' => 'invoice-1',
            'application_id' => 'app-1',
            'fee_type_code' => 'ANNUAL_MEMBERSHIP_FEE',
            'status' => 'PAID',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('payments')->insert([
            'id' => 'payment-1',
            'invoice_id' => 'invoice-1',
            'status' => 'PAID',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('contracts')->insert([
            'id' => 'contract-1',
            'application_id' => 'app-1',
            'status' => 'ACCEPTED',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('roles')->insert([
            'id' => 'role-client',
            'code' => 'CLIENT',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}