<commit_before>%%%-------------------------------------------------------------------
%%% This file is part of the CernVM File System.
%%%
%%% @doc
%%%
%%% @end
%%%
%%%-------------------------------------------------------------------

-module(cvmfs_auth_SUITE).

-include_lib("common_test/include/ct.hrl").

-export([all/0, groups/0, init_per_suite/1, end_per_suite/1, init_per_testcase/2, end_per_testcase/2]).
-export([add_repo/1, remove_repo/1
        ,add_user/1, remove_user/1
        ,list_repos/1, list_users/1
        ,valid_username_valid_paths/1
        ,valid_username_invalid_paths/1
        ,valid_username_no_paths/1
        ,invalid_username_error/1]).


%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% TESTS DESCRIPTIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
all() ->
    [{group, queries}
    ,{group, repo_operations}
    ,{group, user_operations}].

groups() ->
    [{queries, [parallel], [list_repos
                   ,valid_username_valid_paths
                   ,valid_username_invalid_paths
                   ,valid_username_no_paths
                   ,invalid_username_error]}
    ,{repo_operations, [], [add_repo, remove_repo]}
    ,{user_operations, [], [add_user, remove_user]}].


%% Set up, tear down

init_per_suite(Config) ->
    application:start(mnesia),
    ok = ct:require(repos),
    ok = ct:require(acl),
    Watcher = spawn(fun() ->
                            {ok, _} = cvmfs_auth:start_link({ct:get_config(repos),
                                                             ct:get_config(acl)}),
                            receive
                                test_suite_end ->
                                    cvmfs_auth:stop()
                            end
                    end),
    [{watcher_process, Watcher} | Config].

end_per_suite(Config) ->
    Watcher = ?config(watcher_process, Config),
    Watcher ! test_suite_end,
    application:stop(mnesia),
    ok.

init_per_testcase(_TestCase, _Config) ->
    [].

end_per_testcase(_TestCase, _Config) ->
    ok.

%% Test cases follow

list_repos(_Config) ->
    Repos1 = lists:sort(cvmfs_auth:get_repos()),
    Repos2 = lists:sort(lists:foldl(fun({N, _}, Acc) -> [N | Acc] end, [], ct:get_config(repos))),
    lists:zipwith(fun(A, B) -> A = B end, Repos1, Repos2).

list_users(_Config) ->
    Users1 = lists:sort(cvmfs_auth:get_users()),
    Users2 = lists:sort(lists:foldl(fun({U, _}, Acc) -> [U | Acc] end, [], ct:get_config(acl))),
    lists:zipwith(fun(A, B) -> A = B end, Users1, Users2).

valid_username_valid_paths(_Config) ->
    {ok, Results} = cvmfs_auth:get_user_permissions(<<"user1">>),
    Results = [<<"/path/to/repo/1">>, <<"/path/to/another/repo">>, <<"/path/to/last/repo">>].

valid_username_invalid_paths(_Config) -><commit_msg>Simplify some test cases in cvmfs_auth_SUITE<commit_after>%%%-------------------------------------------------------------------
%%% This file is part of the CernVM File System.
%%%
%%% @doc
%%%
%%% @end
%%%
%%%-------------------------------------------------------------------

-module(cvmfs_auth_SUITE).

-include_lib("common_test/include/ct.hrl").

-export([all/0, groups/0, init_per_suite/1, end_per_suite/1, init_per_testcase/2, end_per_testcase/2]).
-export([add_repo/1, remove_repo/1
        ,add_user/1, remove_user/1
        ,list_repos/1, list_users/1
        ,valid_username_valid_paths/1
        ,valid_username_invalid_paths/1
        ,valid_username_no_paths/1
        ,invalid_username_error/1]).


%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% TESTS DESCRIPTIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
all() ->
    [{group, queries}
    ,{group, repo_operations}
    ,{group, user_operations}].

groups() ->
    [{queries, [parallel], [list_repos
                   ,valid_username_valid_paths
                   ,valid_username_invalid_paths
                   ,valid_username_no_paths
                   ,invalid_username_error]}
    ,{repo_operations, [], [add_repo, remove_repo]}
    ,{user_operations, [], [add_user, remove_user]}].


%% Set up, tear down

init_per_suite(Config) ->
    application:start(mnesia),
    ok = ct:require(repos),
    ok = ct:require(acl),
    Watcher = spawn(fun() ->
                            {ok, _} = cvmfs_auth:start_link({ct:get_config(repos),
                                                             ct:get_config(acl)}),
                            receive
                                test_suite_end ->
                                    cvmfs_auth:stop()
                            end
                    end),
    [{watcher_process, Watcher} | Config].

end_per_suite(Config) ->
    Watcher = ?config(watcher_process, Config),
    Watcher ! test_suite_end,
    application:stop(mnesia),
    ok.

init_per_testcase(_TestCase, _Config) ->
    [].

end_per_testcase(_TestCase, _Config) ->
    ok.

%% Test cases follow

list_repos(_Config) ->
    Repos1 = lists:sort(cvmfs_auth:get_repos()),
    Repos2 = lists:sort(lists:foldl(fun({N, _}, Acc) -> [N | Acc] end, [], ct:get_config(repos))),
    Repos1 = Repos2.

list_users(_Config) ->
    Users1 = lists:sort(cvmfs_auth:get_users()),
    Users2 = lists:sort(lists:foldl(fun({U, _}, Acc) -> [U | Acc] end, [], ct:get_config(acl))),
    Users1 = Users2.

valid_username_valid_paths(_Config) ->
    {ok, P1} = cvmfs_auth:get_user_permissions(<<"user1">>),
    Paths1 = lists:sort(P1),
    Paths2 = lists:sort(lists:foldl(fun({_, P}, Acc) -> [P | Acc] end, [], ct:get_config(repos))),
    Paths1 = Paths2.

valid_username_invalid_paths(_Config) ->
{'fraction_non_alphanumeric': 0.20869088917290488, 'fraction_numerical': 0.010407157202848275, 'mean_word_length': 2.1774941995359627, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3131822', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1754, 'n_words': 273}