> For the complete documentation index, see [llms.txt](https://docs.opensoutheners.com/oss/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opensoutheners.com/oss/laravel-scim/testing.md).

# Testing

Tips for testing your SCIM integration in your Laravel application.

## Test setup

In your test base class, you need to:

1. Allow SCIM gate checks
2. Authenticate a user (since Gate needs a user context)

```php
<?php

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Gate;
use App\Models\User;

abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        // Allow all SCIM gate checks in tests
        Gate::before(fn ($user, $ability) => true);

        // Authenticate a user for Gate context
        $this->actingAs(User::factory()->create());
    }
}
```

## Testing user provisioning

```php
public function test_scim_creates_user(): void
{
    $response = $this->postJson('/scim/v2/Users', [
        'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],
        'userName' => 'jane@example.com',
        'name' => 'Jane Doe',
    ]);

    $response->assertCreated();

    $this->assertDatabaseHas('users', [
        'email' => 'jane@example.com',
        'name' => 'Jane Doe',
    ]);
}
```

## Testing user updates

```php
public function test_scim_updates_user_via_put(): void
{
    $user = User::factory()->create([
        'email' => 'old@example.com',
        'name' => 'Old Name',
    ]);

    $response = $this->putJson("/scim/v2/Users/{$user->id}", [
        'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],
        'userName' => 'new@example.com',
        'name' => 'New Name',
    ]);

    $response->assertOk();

    $this->assertDatabaseHas('users', [
        'email' => 'new@example.com',
    ]);
}
```

## Testing PATCH operations

```php
public function test_scim_patches_group_members(): void
{
    $user = User::factory()->create(['name' => 'Alice']);
    $group = Group::factory()->create(['name' => 'Engineering']);

    $response = $this->patchJson("/scim/v2/Groups/{$group->id}", [
        'schemas' => ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
        'Operations' => [
            [
                'op' => 'add',
                'path' => 'members',
                'value' => [
                    ['value' => (string) $user->id, 'display' => 'Alice'],
                ],
            ],
        ],
    ]);

    $response->assertOk();

    $this->assertDatabaseHas('group_user', [
        'user_id' => $user->id,
        'group_id' => $group->id,
    ]);
}
```

## Testing list with filters

```php
public function test_scim_filters_users(): void
{
    User::factory()->create(['email' => 'alice@example.com']);
    User::factory()->create(['email' => 'bob@example.com']);

    $response = $this->getJson(
        '/scim/v2/Users?filter=' . urlencode('userName eq "alice@example.com"')
    );

    $response->assertOk();
    $response->assertJsonCount(1, 'Resources');
}
```

## Testing error responses

```php
public function test_scim_validation_error_format(): void
{
    $response = $this->postJson('/scim/v2/Users', [
        'schemas' => ['urn:ietf:params:scim:schemas:core:2.0:User'],
        // Missing required userName
    ]);

    $response->assertStatus(400);

    $response->assertJsonFragment([
        'schemas' => ['urn:ietf:params:scim:api:messages:2.0:Error'],
        'status' => '400',
        'scimType' => 'invalidValue',
    ]);
}
```

## Testing with named routes

You can also use named routes:

```php
$this->getJson(route('scim.v2.SchemaActions.index', ['schema' => 'Users']));
$this->getJson(route('scim.v2.SchemaActions.show', ['schema' => 'Users', 'id' => $user->id]));
$this->postJson(route('scim.v2.SchemaActions.store', ['schema' => 'Users']), $data);
$this->putJson(route('scim.v2.SchemaActions.update', ['schema' => 'Users', 'id' => $user->id]), $data);
$this->deleteJson(route('scim.v2.SchemaActions.destroy', ['schema' => 'Users', 'id' => $user->id]));
```

## Route names reference

| Route name                      | Description                 |
| ------------------------------- | --------------------------- |
| `scim.v2.ServiceProviderConfig` | Service provider config     |
| `scim.v2.ResourceTypes`         | Resource types list         |
| `scim.v2.Schemas.index`         | All schemas                 |
| `scim.v2.Schemas.show`          | Single schema               |
| `scim.v2.SchemaActions.index`   | List resources              |
| `scim.v2.SchemaActions.show`    | Get single resource         |
| `scim.v2.SchemaActions.store`   | Create resource             |
| `scim.v2.SchemaActions.update`  | Update resource (PUT/PATCH) |
| `scim.v2.SchemaActions.destroy` | Delete resource             |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.opensoutheners.com/oss/laravel-scim/testing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
