seed(RolePermissionSeeder::class); $this->actingAs($this->userWithRoles('editor')); config([ 'services.taskstream.api_url' => $this->taskstream, 'services.taskstream.web_url' => 'https://taskstream.test', ]); // What signing in to the panel leaves in the session. app(TokenStore::class)->put(new AccessToken('session-token', null, now()->addHour()->toImmutable())); } protected function fakeTaskStream(int $status = 201, array $body = []): void { Http::fake([ "{$this->taskstream}/api/projects" => fn (Request $request) => Http::response( $status === 201 ? ['id' => 'ts-1', 'key' => $request['key'], 'version' => 1] : $body, $status, ), ]); } #[Test] public function the_toggle_opens_a_board_with_the_session_token(): void { $this->fakeTaskStream(); Livewire::test(CreateProject::class) ->fillForm([ 'title' => ['es' => 'Catálogo abierto'], 'slug' => ['es' => 'catalogo'], 'taskstream' => true, ]) ->assertFormFieldExists('taskstream_key') ->call('create') ->assertHasNoFormErrors() ->assertNotified(); Http::assertSent(fn (Request $request) => $request->url() === "{$this->taskstream}/api/projects" && $request->hasHeader('Authorization', 'Bearer session-token') && $request['key'] === 'CATALOGO' && $request['isPublic'] === true); $project = Project::query()->sole(); $this->assertSame('CATALOGO', $project->taskstream_key); Livewire::test(EditProject::class, ['record' => $project->getRouteKey()]) ->assertActionVisible('taskstream') ->assertActionHasUrl('taskstream', 'https://taskstream.test/p/CATALOGO') ->assertFormFieldDoesNotExist('taskstream'); } #[Test] public function without_the_toggle_taskstream_is_left_alone(): void { $this->fakeTaskStream(); Livewire::test(CreateProject::class) ->fillForm(['title' => ['es' => 'Solo aquí'], 'slug' => ['es' => 'solo-aqui']]) ->assertFormFieldHidden('taskstream_key') ->call('create') ->assertHasNoFormErrors(); Http::assertNothingSent(); Livewire::test(EditProject::class, ['record' => Project::query()->sole()->getRouteKey()]) ->assertActionHidden('taskstream'); } #[Test] public function a_refusal_shows_on_the_key_and_saves_nothing(): void { $this->fakeTaskStream(409, ['statusCode' => 409, 'message' => 'Project key WEB is already in use']); Livewire::test(CreateProject::class) ->fillForm([ 'title' => ['es' => 'Web'], 'slug' => ['es' => 'web'], 'taskstream' => true, 'taskstream_key' => 'web', ]) ->call('create') ->assertHasFormErrors(['taskstream_key']); $this->assertSame(0, Project::withTrashed()->count()); } #[Test] public function taskstream_being_down_saves_nothing(): void { Http::fake(["{$this->taskstream}/*" => Http::failedConnection()]); Livewire::test(CreateProject::class) ->fillForm(['title' => ['es' => 'Web'], 'slug' => ['es' => 'web'], 'taskstream' => true]) ->call('create') ->assertNotified(__('rules.taskstream.unavailable')); $this->assertSame(0, Project::withTrashed()->count()); } }