<?php

namespace Tests\Feature;

use App\Models\Asns;
use App\Models\Group;
use App\Models\Mercury\MercuryOrganisationAttributes;
use App\Models\Organisation;
use App\Models\OrganisationExternalIdentities;
use App\Models\Prefixes;
use App\Models\PrefixSubtenants;
use App\Models\ProductRights;
use App\Models\Suborganisation;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

/**
 *      @@@ TEST CERTIFICATION @@@
 *      This set of tests has been confirmed to be functioning as intended.
 *      
 *      Jesse Cunningham, 2026-04-21 4:00 PM AEST
 */

class InternalWhmcsApiTest extends TestCase
{

    use RefreshDatabase;

    private $gsl = null;
    private $gd = null;
    private $sl = null;
    private $read_only_group = null;
    private $write_access_group = null;

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

        $this->gsl = Organisation::create([ 'id' => 1, 'name' => config('creatia.automation.main_organisation_name') ]);
        $this->gd = Organisation::create([ 'id' => 2, 'name' => 'Go Dedicated' ]);
        $this->sl = Organisation::create([ 'id' => 3, 'name' => 'Streamline Servers' ]);

        DB::statement("SELECT setval('organisations_id_seq', (SELECT MAX(id) FROM organisations))");

        // Give GSL an ASN and some default prefixes.
        $asn = Asns::create([ 'asn' => '137409', 'organisation_id' => $this->gsl->id, 'approved' => 1 ]);

        // Prefixes
        $to_make = [
            [ 'address' => '103.1.0.0', 'mask' => 24 ],
            [ 'address' => '103.1.0.0', 'mask' => 25 ],
            [ 'address' => '103.2.0.0', 'mask' => 16 ],
            [ 'address' => '103.1.184.0', 'mask' => 22 ],
            [ 'address' => '131.242.0.0', 'mask' => 16 ],
            [ 'address' => '147.132.0.0', 'mask' => 16 ],
            [ 'address' => '161.143.0.0', 'mask' => 16 ],
            [ 'address' => '164.108.8.0', 'mask' => 22 ],
            [ 'address' => '164.108.12.0', 'mask' => 23 ],
            [ 'address' => '164.108.16.0', 'mask' => 24 ],
            [ 'address' => '164.108.18.0', 'mask' => 24 ],
            [ 'address' => '164.108.104.0', 'mask' => 24 ]
        ];

        $user_to_make = [
            [ 'address' => '164.108.104.0', 'mask' => 29 ],
            [ 'address' => '164.108.104.0', 'mask' => 30 ],
            [ 'address' => '164.108.104.0', 'mask' => 31 ],
            [ 'address' => '164.108.104.0', 'mask' => 32 ],
            [ 'address' => '164.108.104.1', 'mask' => 32 ],
            [ 'address' => '164.108.104.2', 'mask' => 32 ],
            [ 'address' => '164.108.104.3', 'mask' => 32 ]
        ];

        foreach($to_make as $x) {
            DB::table("prefixes")->insert([
                [
                    'seen_at'           => Carbon::now(),
                    'still_announced'   => 1,
                    'created_source'    => 'prefix-list-uncrushed',
                    'organisation_id'   => $this->gsl->id,
                    'asn_id'            => $asn->id,
                    'setup_required'    => 0,
                    ...$x
                ]
            ]);
        }

        foreach($user_to_make as $x) {
            DB::table("prefixes")->insert([
                [
                    'seen_at'           => Carbon::now(),
                    'still_announced'   => 1,
                    'created_source'    => 'user',
                    'organisation_id'   => $this->gsl->id,
                    'asn_id'            => $asn->id,
                    'setup_required'    => 0,
                    ...$x
                ]
            ]);
        }

        $this->write_access_group = Group::create([ 'name' => 'Creatia Standard User' ]);
        $this->read_only_group = Group::create([ 'name' => 'Creatia Read-Only User' ]);
    }

    public function testOrganisationProvision()
    {

        $response_1 = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1121' ],
            ],
            'subtenant' => 'gsl'
        ]);

        $response_2 = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation B',
            'identifiers' => [
                [ 'key' => 'gd-whmcs', 'identity' => '2221' ],
            ],
            'subtenant' => 'go-dedicated'
        ]);

        $response_3 = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation C',
            'identifiers' => [
                [ 'key' => 'sl-whmcs', 'identity' => '3321' ],
            ],
            'subtenant' => 'streamline'
        ]);

        // Check that everything has the correct suborganisational entry.

        $this->assertEquals(0, Suborganisation::where([['child_id', '=', $response_1->json()['detail']], ['parent_id', '=', $this->gsl->id]])->count(), 'Organisation A did not configure correctly.');
        $this->assertEquals(1, Suborganisation::where([['child_id', '=', $response_2->json()['detail']], ['parent_id', '=', $this->gd->id]])->count(), 'Organisation B did not configure correctly.');
        $this->assertEquals(1, Suborganisation::where([['child_id', '=', $response_3->json()['detail']], ['parent_id', '=', $this->sl->id]])->count(), 'Organisation C did not configure correctly.');

    }

    public function testOrganisationExternalIdentities()
    {

        $this->testOrganisationProvision();

        // Check whether the entries are correctly returned. 
        $identities_1 = $this->withoutMiddleware()->get('/api/internal/organisation?application=gsl-whmcs')->json();
        $identities_2 = $this->withoutMiddleware()->get('/api/internal/organisation?application=gd-whmcs')->json();
        $identities_3 = $this->withoutMiddleware()->get('/api/internal/organisation?application=sl-whmcs')->json();

        $this->assertTrue($identities_1['data'][0]['external_id'] === '1121', 'The GSL-WHMCS identity needs to be 1121.');
        $this->assertTrue($identities_2['data'][0]['external_id'] === '2221', 'The GD-WHMCS identity needs to be 2221.');
        $this->assertTrue($identities_3['data'][0]['external_id'] === '3321', 'The SL-WHMCS identity needs to be 3321.');

        // Check whether specific values will be accurately honed in upon. Add some new entries to make sure this works correctly.
        $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation D',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1122' ],
            ],
            'subtenant' => 'gsl'
        ]);

        $identities_4 = $this->withoutMiddleware()->get('/api/internal/organisation?application=gsl-whmcs&identity=1122')->json();

        $this->assertTrue(sizeof($identities_4['data']) === 1, 'The GSL-WHMCS identity returned needs to be 1122.');
    }

    public function testOrganisationPrefixCreate()
    {

        $this->testOrganisationProvision();

        // We have a few paths we need to test - Prefix creation, prefix association, subtenancy types.
        $identity = $this->withoutMiddleware()->get('/api/internal/organisation?application=gd-whmcs&identity=2221')->json()['data'][0]['creatia_id'];

        // Test 1 - Assign prefix to Go Dedicated, then assign it to the suborganisation.
        $response_1 = $this->withoutMiddleware()->post("/api/internal/organisation/$identity/network", [
            'subnet'            => '103.1.0.0/25',
            'name'              => 'GD Test',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'go-dedicated'
        ])->json();

        $prefix_1 = Prefixes::where([['id', '=', $response_1['detail']]])->get()->first() ?? null;
        $prefix_1_subtenancy = PrefixSubtenants::where('prefix_id', '=', $response_1['detail'])->get();

        $this->assertTrue($prefix_1 !== null && $prefix_1_subtenancy->where('subtenant_id', '=', $identity)->count() === 2 && $prefix_1_subtenancy->where('subtenant_id', '=', $this->gd->id)->count() === 2, 'The first prefix creation test failed.');

        // Test 2 - Create a prefix and assign it to Go Dedicated then assign it to the suborganisation.
        $response_2 = $this->withoutMiddleware()->post("/api/internal/organisation/$identity/network", [
            'subnet'            => '103.1.0.32/32',
            'name'              => 'GD Test',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'go-dedicated'
        ])->json();

        $prefix_2 = Prefixes::where('id', '=', $response_2['detail'])->get()->first() ?? null;
        $prefix_2_subtenancy = PrefixSubtenants::where('prefix_id', '=', $response_2['detail'])->get();

        $this->assertTrue($prefix_2 !== null && $prefix_2_subtenancy->where('subtenant_id', '=', $identity)->count() === 2 && $prefix_2_subtenancy->where('subtenant_id', '=', $this->gd->id)->count() === 2, 'The second prefix creation test failed.');

        // Test 3 - Create a prefix and test that GSL still functions as desired
        $response_3 = $this->withoutMiddleware()->post("/api/internal/organisation/$identity/network", [
            'subnet'            => '103.1.0.32/32',
            'name'              => 'GSL Test',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'gsl'
        ])->json();

        $prefix_3 = Prefixes::where('id', '=', $response_3['detail'])->get()->first() ?? null;
        $prefix_3_subtenancy = PrefixSubtenants::where('prefix_id', '=', $response_3['detail'])->count();

        $this->assertTrue($prefix_3 !== null && $prefix_3_subtenancy === 2, 'The third prefix creation test failed.');

        // Test 4 - Create a prefix using overrides
        $response_4 = $this->withoutMiddleware()->post("/api/internal/organisation/$identity/network", [
            'subnet'            => '203.1.0.32/32',
            'name'              => 'GSL Test Override',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => true,
            'brand'             => 'gsl'
        ])->json();

        $prefix_4 = Prefixes::where('id', '=', $response_4['detail'])->get()->first() ?? null;
        $prefix_4_subtenancy = PrefixSubtenants::where('prefix_id', '=', $response_4['detail'])->count();

        $this->assertTrue($prefix_4 !== null && $prefix_4_subtenancy === 2, 'The fourth prefix creation test failed.');

        // Test 5 - Handle deletion of child prefixes.
        
        $response_5 = $this->withoutMiddleware()->post("/api/internal/organisation/$identity/network", [
            'subnet'            => '164.108.104.0/30',
            'name'              => 'Remove Child Prefixes',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'go-dedicated'
        ])->json();

        // Determine whether or not prefixes were deleted.
        $num_children_deleted = Prefixes::onlyTrashed()->where(DB::raw("'164.108.104.0/30'::cidr"), '>>=', DB::raw('CONCAT(address, \'/\', mask)::cidr'))->get()->count();

        $this->assertEquals(6, $num_children_deleted, 'Six children should have been deleted.');

        // Determine whether or not the appropriate prefix was created.
        $prefix_5 = Prefixes::where([['id', '=', $response_5['detail']]])->get()->first() ?? null;
        $prefix_5_subtenancy = PrefixSubtenants::where('prefix_id', '=', $response_5['detail'])->get();

        $this->assertTrue($prefix_5 !== null && $prefix_5_subtenancy->where('subtenant_id', '=', $identity)->count() === 2 && $prefix_5_subtenancy->where('subtenant_id', '=', $this->gd->id)->count() === 2, 'The fifth prefix creation test failed.');

    }

    public function testNetworkListPrefixes()
    {
        // Create two organisations - We want to make sure when we segregate things it behaves appropirately.
        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1121' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $organisation_b = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation B',
            'identifiers' => [
                [ 'key' => 'gd-whmcs', 'identity' => '1121' ],
            ],
            'subtenant' => 'go-dedicated'
        ])->json()['detail'];

        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/network", [
            'subnet'            => '103.1.0.0/25',
            'name'              => 'GSL Test 1',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'gsl'
        ])->json();
        
        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/network", [
            'subnet'            => '103.1.0.32/32',
            'name'              => 'GSL Test 2',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'gsl'
        ])->json();

        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_b/network", [
            'subnet'            => '164.108.104.0/32',
            'name'              => 'GD Test 1',
            'prefix_only'       => true,
            'children_allowed'  => true,
            'override'          => false,
            'brand'             => 'go-dedicated'
        ])->json();

        $list_1 = $this->withoutMiddleware()->get("/api/internal/organisation/$organisation_a/network")->json();

        $found_count = 0;

        foreach($list_1['data'] as $prefix) {
            if($prefix['prefix'] === '103.1.0.0/25' || $prefix['prefix'] === '103.1.0.32/32') {
                ++$found_count;
            }
        }

        $this->assertTrue($found_count === 2 && sizeof($list_1['data']) === 2, 'The prefix list should have only returned two prefixes.');
    }

    public function testOrganisationUserAdd()
    {

        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $organisation_b = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation B',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '2' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $user_a = $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/user", [
            'name' => 'User A',
            'email' => 'test+a@globalsecurelayer.com'
        ])->json()['detail'];

        $user_b = $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_b/user", [
            'name' => 'User B',
            'email' => 'test+b@globalsecurelayer.com'
        ])->json()['detail'];
        
        $this->assertTrue(User::find($user_a)->organisation_id == $organisation_a, 'The user created either did not associate with the correct organisation, or it was not created at all.');
        $this->assertTrue(User::find($user_b)->organisation_id == $organisation_b, 'The user created either did not associate with the correct organisation, or it was not created at all.');

        $this->assertTrue(User::find($user_a)->group_id == $this->write_access_group->id, 'The user has an inappropriate security group.');
        $this->assertTrue(User::find($user_b)->group_id == $this->write_access_group->id, 'The user has an inappropriate security group.');

        $this->assertTrue(User::whereIn('organisation_id', [ $organisation_a, $organisation_b ])->count() === 2, 'Too many users were created.');

    }

    public function testOrganisationUserV2Add()
    {

        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $organisation_b = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation B',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '2' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $user_a = $this->withoutMiddleware()->post("/api/internal/v2/organisation/$organisation_a/user", [
            'name'      => 'User A',
            'email'     => 'test+a@globalsecurelayer.com',
            'readonly'  => false
        ])->json()['detail'];

        $user_b = $this->withoutMiddleware()->post("/api/internal/v2/organisation/$organisation_b/user", [
            'name'      => 'User B',
            'email'     => 'test+b@globalsecurelayer.com',
            'readonly'  => true
        ])->json()['detail'];
        
        $this->assertTrue(User::find($user_a)->organisation_id == $organisation_a, 'The user created either did not associate with the correct organisation, or it was not created at all.');
        $this->assertTrue(User::find($user_b)->organisation_id == $organisation_b, 'The user created either did not associate with the correct organisation, or it was not created at all.');

        $this->assertTrue(User::find($user_a)->group_id == $this->write_access_group->id, 'The user has an inappropriate security group. It should have write access.');
        $this->assertTrue(User::find($user_b)->group_id == $this->read_only_group->id, 'The user has an inappropriate security group. It should be read only.');

        $this->assertTrue(User::whereIn('organisation_id', [ $organisation_a, $organisation_b ])->count() === 2, 'Too many users were created.');

    }

    public function testOrganisationUpdate()
    {

        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $this->withoutMiddleware()->put("/api/internal/organisation/$organisation_a", [
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '2' ],
                [ 'key' => 'gd-whmcs', 'identity' => '3' ],
                [ 'key' => 'sl-whmcs', 'identity' => '4' ],
            ]
        ]);

        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'gsl-whmcs' ], [ 'identity', '=', '2' ]])->count() === 1, 'The `gsl-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'gd-whmcs' ], [ 'identity', '=', '3' ]])->count() === 1, 'The `gd-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'sl-whmcs' ], [ 'identity', '=', '4' ]])->count() === 1, 'The `sl-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where('organisation_id', '=', $organisation_a)->count() === 3, 'There should be only three external identities but there is more.');
    }

    public function testOrganisationV2Update()
    {

        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $organisation_b = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation B',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '123' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $this->withoutMiddleware()->put("/api/internal/v2/organisation/$organisation_a", [
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1', 'mode' => 'remove' ],
                [ 'key' => 'gsl-whmcs', 'identity' => '2', 'mode' => 'add' ],
                [ 'key' => 'gd-whmcs', 'identity' => '3', 'mode' => 'add' ],
                [ 'key' => 'sl-whmcs', 'identity' => '4', 'mode' => 'add' ],
            ]
        ]);

        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'gsl-whmcs' ], [ 'identity', '=', '2' ]])->exists(), 'The `gsl-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'gd-whmcs' ], [ 'identity', '=', '3' ]])->exists(), 'The `gd-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'sl-whmcs' ], [ 'identity', '=', '4' ]])->exists(), 'The `sl-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where('organisation_id', '=', $organisation_a)->count() === 3, 'There should be only three external identities but there is more.');

        $this->withoutMiddleware()->put("/api/internal/v2/organisation/$organisation_a", [
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '2', 'mode' => 'remove' ]
            ]
        ]);

        $this->assertTrue(!OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'gsl-whmcs' ], [ 'identity', '=', '2' ]])->exists(), 'The `gsl-whmcs` identity should have been removed.');
        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'gd-whmcs' ], [ 'identity', '=', '3' ]])->exists(), 'The `gd-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where([['organisation_id', '=', $organisation_a], [ 'key', '=', 'sl-whmcs' ], [ 'identity', '=', '4' ]])->exists(), 'The `sl-whmcs` identity did not generate correctly.');
        $this->assertTrue(OrganisationExternalIdentities::where('organisation_id', '=', $organisation_a)->count() === 2, 'There should be only three external identities but there is more.');
    }

    public function testMercuryUpdate()
    {
        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $this->withoutMiddleware()->put("/api/internal/organisation/$organisation_a/mercury", [
            'bandwidth' => 123
        ]);

        $this->assertTrue(MercuryOrganisationAttributes::where([ ['organisation_id', '=', $organisation_a], ['aggregate_bandwidth', '=', 123] ])->count() === 1, 'The bandwidth did not assign correctly.');

    }

    public function testAddRight()
    {
        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/right", [
            'right' => 'a'
        ]);

        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/right", [
            'right' => 'b'
        ]);

        $this->assertTrue(ProductRights::where([ ['organisation_id', '=', $organisation_a], ['name', '=', 'a'] ])->count() === 1, 'Right `a` was not granted.');
        $this->assertTrue(ProductRights::where([ ['organisation_id', '=', $organisation_a], ['name', '=', 'b'] ])->count() === 1, 'Right `b` was not granted.');

    }

    public function testRemoveRight()
    {
        $organisation_a = $this->withoutMiddleware()->post('/api/internal/organisation', [
            'name' => 'Organisation A',
            'identifiers' => [
                [ 'key' => 'gsl-whmcs', 'identity' => '1' ],
            ],
            'subtenant' => 'gsl'
        ])->json()['detail'];

        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/right", [
            'right' => 'a'
        ]);

        $this->withoutMiddleware()->post("/api/internal/organisation/$organisation_a/right", [
            'right' => 'b'
        ]);

        $this->withoutMiddleware()->delete("/api/internal/organisation/$organisation_a/right", [
            'right' => 'a'
        ]);

        $this->assertTrue(ProductRights::where([ ['organisation_id', '=', $organisation_a], ['name', '=', 'a'] ])->count() === 0, 'Right `a` was not granted.');
        $this->assertTrue(ProductRights::where([ ['organisation_id', '=', $organisation_a], ['name', '=', 'b'] ])->count() === 1, 'Right `b` was not granted.');


    }

}
