<commit_before>package main;
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use DBI;
use JSON;
use Digest::SHA1 qw(sha1_hex);
use Data::Dumper;
use strict;
use warnings;
no warnings 'once';
use warnings 'all';
use Test::TestHelper;
use Test::TestHelper;
use Utils::Tenant;

#no_transactions=>1 ==> keep fixtures after every execution, beware of duplicate data!
#no_transactions=>0 ==> delete fixtures after every execution

BEGIN { $ENV{MOJO_MODE} = "test" }

my $schema = Schema->connect_to_database;
my $dbh    = Schema->database_handle;
my $t      = Test::Mojo->new('TrafficOps');

Test::TestHelper->unload_core_data($schema);
Test::TestHelper->load_core_data($schema);

#globals
my $root_tenant_id = get_tenant_id("root");

# building the below heirarchy,
# and creating an admin user for each tenant
#
# -root
#     |---A
#     |   |---A1
#     |   |   |
#     |   |   |---A1a
#     |   |   |---A1b, in-active
#     |   |
#     |   |---A2
#     |   |   |
#     |   |   |---A2a
#     |   |
#     |   |---A3, in-active
#     |   |   |
#     |   |   |---A3a
#     |
#     |---B
#     |   |---B1

# Count the 'response number'
my $responses_counter = sub {
    my $t = shift;
    my $json = decode_json( $t->tx->res->content->asset->slurp );
    my $r    = $json->{response};
    if ($r) {
        return scalar(@$r);
    }
    return 0;
};

# Count the 'response number', and compare to the give value
my $count_response_test = sub {
    my ( $t, $count ) = @_;
    return $t->success( is( $t->$responses_counter(), $count ) );
};

#Get the null tenants counters
ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_USER, p => Test::TestHelper::ADMIN_USER_PASSWORD } )->status_is(302)
        ->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should login?';

my $fixture_num_of_tenants = $t->get_ok('/api/1.2/tenants')->status_is(200)->$responses_counter();
my $fixture_num_of_users = $t->get_ok('/api/1.2/users')->status_is(200)->$responses_counter();

ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content}; } );


#Building up the setup
ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_ROOT_USER, p => Test::TestHelper::ADMIN_ROOT_USER_PASSWORD } )->status_is(302)
        ->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should login?';

#verifying the basic cfg
ok $t->get_ok("/api/1.2/tenants")->status_is(200)->json_is( "/response/0/name", "root" )->or( sub { diag $t->tx->res->content->asset->{content}; } );;

my $tenants_data = {};
prepare_tenant("root", undef, $tenants_data);
prepare_tenant("none", undef, $tenants_data);
prepare_tenant("A", $root_tenant_id, $tenants_data);
prepare_tenant("A1", $tenants_data->{"A"}->{'id'}, $tenants_data);
prepare_tenant("A1a", $tenants_data->{"A1"}->{'id'}, $tenants_data);
prepare_tenant("A1b", $tenants_data->{"A1"}->{'id'}, $tenants_data);
prepare_tenant("A2", $tenants_data->{"A"}->{'id'}, $tenants_data);
prepare_tenant("A2a", $tenants_data->{"A2"}->{'id'}, $tenants_data);
prepare_tenant("A3", $tenants_data->{"A"}->{'id'}, $tenants_data);
prepare_tenant("A3a", $tenants_data->{"A3"}->{'id'}, $tenants_data);
prepare_tenant("B", $root_tenant_id, $tenants_data);
deactivate_tenant("A1b", $tenants_data);
deactivate_tenant("A3", $tenants_data);


ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content}; } );



########################################################################################
# All is ready - lets start testing
########################################################################################
#####Working as user from tenant "A1"
login_to_tenant_admin ("A1", $tenants_data);
my $num_of_tenants_can_be_accessed = 3; #A1, A1a, A1b
#sanity check on tenants - testing of tenant as a resource is taken care of in tenants.t
ok $t->get_ok('/api/1.2/tenants')->status_is(200)->$count_response_test($num_of_tenants_can_be_accessed+$fixture_num_of_tenants);
#cannot change its tenancy
ok $t->put_ok('/api/1.2/user/current' => {Accept => 'application/json'} =>
        json => { user => { tenantId => $tenants_data->{"A1a"}->{'id'}} } )
        ->json_is( "/alerts/0/text" => "Cannot change user tenancy")
        ->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )
    , 'Cannot change my tenancy: tenant: A1?';

logout_from_tenant_admin();
#access to himself
test_user_resource_read_allow_access ("A1", "A1", $tenants_data);
test_user_resource_write_allow_access("A1", "A1", $tenants_data);
#access to child
test_user_resource_read_allow_access ("A1", "A1a", $tenants_data);
test_user_resource_write_allow_access("A1", "A1a", $tenants_data);
#access to even if child is inactive
test_user_resource_read_allow_access ("A1", "A1a", $tenants_data);
test_user_resource_write_allow_access("A1", "A1a", $tenants_data);
#No access to parent
test_user_resource_read_block_access ("A1", "A", $tenants_data);
test_user_resource_write_block_access("A1", "A", $tenants_data);
#No access to brother
test_user_resource_read_block_access ("A1", "A2", $tenants_data);
test_user_resource_write_block_access("A1", "A2", $tenants_data);
#No access to nephew
test_user_resource_read_block_access ("A1", "A2a", $tenants_data);
test_user_resource_write_block_access("A1", "A2a", $tenants_data);
#No access to uncle
test_user_resource_read_block_access ("A1", "B", $tenants_data);
test_user_resource_write_block_access("A1", "B", $tenants_data);
#No access to grandfather
test_user_resource_read_block_access ("A1", "root", $tenants_data);
test_user_resource_write_block_access("A1", "root", $tenants_data);
#access to "no-tenant"
test_user_resource_read_allow_access ("A1", "none", $tenants_data);
test_user_resource_write_allow_access("A1", "none", $tenants_data);

#####Working as user from inactive tenant "A3"
login_to_tenant_admin ("A3", $tenants_data);
$num_of_tenants_can_be_accessed = 0;
#sanity check on tenants - testing of tenant as a resource is taken care of in tenants.t
ok $t->get_ok('/api/1.2/tenants')->status_is(200)->$count_response_test($num_of_tenants_can_be_accessed+$fixture_num_of_tenants);
#cannot change its tenancy
ok $t->put_ok('/api/1.2/user/current' => {Accept => 'application/json'} =>
        json => { user => { tenantId => $tenants_data->{"A1a"}->{'id'}} } )
        ->json_is( "/alerts/0/text" => "Cannot change user tenancy")
        ->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )
    , 'Cannot change my tenancy: tenant: A1?';
logout_from_tenant_admin();
#no access to anywhere<commit_msg>User tenancy verification - index test<commit_after>package main;
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use Mojo::Base -strict;
use Test::More;
use Test::Mojo;
use DBI;
use JSON;
use Digest::SHA1 qw(sha1_hex);
use Data::Dumper;
use strict;
use warnings;
no warnings 'once';
use warnings 'all';
use Test::TestHelper;
use Test::TestHelper;
use Utils::Tenant;

#no_transactions=>1 ==> keep fixtures after every execution, beware of duplicate data!
#no_transactions=>0 ==> delete fixtures after every execution

BEGIN { $ENV{MOJO_MODE} = "test" }

my $schema = Schema->connect_to_database;
my $dbh    = Schema->database_handle;
my $t      = Test::Mojo->new('TrafficOps');

Test::TestHelper->unload_core_data($schema);
Test::TestHelper->load_core_data($schema);

#globals
my $root_tenant_id = get_tenant_id("root");

# building the below heirarchy,
# and creating an admin user for each tenant
#
# -root
#     |---A
#     |   |---A1
#     |   |   |
#     |   |   |---A1a
#     |   |   |---A1b, in-active
#     |   |
#     |   |---A2
#     |   |   |
#     |   |   |---A2a
#     |   |
#     |   |---A3, in-active
#     |   |   |
#     |   |   |---A3a
#     |
#     |---B
#     |   |---B1

# Count the 'response number'
my $responses_counter = sub {
    my $t = shift;
    my $json = decode_json( $t->tx->res->content->asset->slurp );
    my $r    = $json->{response};
    if ($r) {
        return scalar(@$r);
    }
    return 0;
};

# Count the 'response number', and compare to the give value
my $count_response_test = sub {
    my ( $t, $count ) = @_;
    return $t->success( is( $t->$responses_counter(), $count ) );
};

#Building up the setup
ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_ROOT_USER, p => Test::TestHelper::ADMIN_ROOT_USER_PASSWORD } )->status_is(302)
        ->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should login?';

#verifying the basic cfg
ok $t->get_ok("/api/1.2/tenants")->status_is(200)->json_is( "/response/0/name", "root" )->or( sub { diag $t->tx->res->content->asset->{content}; } );;

my $tenants_data = {};
prepare_tenant("root", undef, $tenants_data);
prepare_tenant("none", undef, $tenants_data);
prepare_tenant("A", $root_tenant_id, $tenants_data);
prepare_tenant("A1", $tenants_data->{"A"}->{'id'}, $tenants_data);
prepare_tenant("A1a", $tenants_data->{"A1"}->{'id'}, $tenants_data);
prepare_tenant("A1b", $tenants_data->{"A1"}->{'id'}, $tenants_data);
prepare_tenant("A2", $tenants_data->{"A"}->{'id'}, $tenants_data);
prepare_tenant("A2a", $tenants_data->{"A2"}->{'id'}, $tenants_data);
prepare_tenant("A3", $tenants_data->{"A"}->{'id'}, $tenants_data);
prepare_tenant("A3a", $tenants_data->{"A3"}->{'id'}, $tenants_data);
prepare_tenant("B", $root_tenant_id, $tenants_data);
deactivate_tenant("A1b", $tenants_data);
deactivate_tenant("A3", $tenants_data);


ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content}; } );

#Get the null tenants counters
ok $t->post_ok( '/login', => form => { u => Test::TestHelper::ADMIN_USER, p => Test::TestHelper::ADMIN_USER_PASSWORD } )->status_is(302)
        ->or( sub { diag $t->tx->res->content->asset->{content}; } ), 'Should login?';

my $fixture_num_of_tenants = $t->get_ok('/api/1.2/tenants')->status_is(200)->$responses_counter();
my $fixture_num_of_users = $t->get_ok('/api/1.2/users')->status_is(200)->$responses_counter();

ok $t->get_ok('/logout')->status_is(302)->or( sub { diag $t->tx->res->content->asset->{content}; } );


########################################################################################
# All is ready - lets start testing
########################################################################################
#####Working as user from tenant "A1"
login_to_tenant_admin ("A1", $tenants_data);
my $num_of_tenants_can_be_accessed = 3; #A1, A1a, A1b
#sanity check on tenants - testing of tenant as a resource is taken care of in tenants.t
ok $t->get_ok('/api/1.2/tenants')->status_is(200)->$count_response_test($num_of_tenants_can_be_accessed+$fixture_num_of_tenants);
ok $t->get_ok('/api/1.2/users')->status_is(200)->$count_response_test($num_of_tenants_can_be_accessed+$fixture_num_of_users);
#cannot change its tenancy
ok $t->put_ok('/api/1.2/user/current' => {Accept => 'application/json'} =>
        json => { user => { tenantId => $tenants_data->{"A1a"}->{'id'}} } )
        ->json_is( "/alerts/0/text" => "Cannot change user tenancy")
        ->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )
    , 'Cannot change my tenancy: tenant: A1?';

logout_from_tenant_admin();
#access to himself
test_user_resource_read_allow_access ("A1", "A1", $tenants_data);
test_user_resource_write_allow_access("A1", "A1", $tenants_data);
#access to child
test_user_resource_read_allow_access ("A1", "A1a", $tenants_data);
test_user_resource_write_allow_access("A1", "A1a", $tenants_data);
#access to even if child is inactive
test_user_resource_read_allow_access ("A1", "A1a", $tenants_data);
test_user_resource_write_allow_access("A1", "A1a", $tenants_data);
#No access to parent
test_user_resource_read_block_access ("A1", "A", $tenants_data);
test_user_resource_write_block_access("A1", "A", $tenants_data);
#No access to brother
test_user_resource_read_block_access ("A1", "A2", $tenants_data);
test_user_resource_write_block_access("A1", "A2", $tenants_data);
#No access to nephew
test_user_resource_read_block_access ("A1", "A2a", $tenants_data);
test_user_resource_write_block_access("A1", "A2a", $tenants_data);
#No access to uncle
test_user_resource_read_block_access ("A1", "B", $tenants_data);
test_user_resource_write_block_access("A1", "B", $tenants_data);
#No access to grandfather
test_user_resource_read_block_access ("A1", "root", $tenants_data);
test_user_resource_write_block_access("A1", "root", $tenants_data);
#access to "no-tenant"
test_user_resource_read_allow_access ("A1", "none", $tenants_data);
test_user_resource_write_allow_access("A1", "none", $tenants_data);

#####Working as user from inactive tenant "A3"
login_to_tenant_admin ("A3", $tenants_data);
$num_of_tenants_can_be_accessed = 0;
#sanity check on tenants - testing of tenant as a resource is taken care of in tenants.t
ok $t->get_ok('/api/1.2/tenants')->status_is(200)->$count_response_test(0);
ok $t->get_ok('/api/1.2/users')->status_is(200)->$count_response_test(0);
#cannot change its tenancy
ok $t->put_ok('/api/1.2/user/current' => {Accept => 'application/json'} =>
        json => { user => { tenantId => $tenants_data->{"A1a"}->{'id'}} } )
        ->json_is( "/alerts/0/text" => "Cannot change user tenancy")
        ->status_is(400)->or( sub { diag $t->tx->res->content->asset->{content}; } )
    , 'Cannot change my tenancy: tenant: A1?';
logout_from_tenant_admin();
#no access to anywhere
{'fraction_non_alphanumeric': 0.20296173275205867, 'fraction_numerical': 0.017576638294927685, 'mean_word_length': 5.168160478019633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 265, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4320760', 'n_tokens_mistral': 5268, 'n_tokens_neox': 5183, 'n_words': 1121}