<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME>
%%% @copyright (C): 2014 ACK CYFRONET AGH
%%% This software is released under the MIT license
%%% cited in 'LICENSE.txt'.
%%% @end
%%%-------------------------------------------------------------------
%%% @doc This file contains tests for user logic module concerning:
%%%   - personal space name mapping for users
%%%   - logging in via basic auth by interacting with onepanel
%%%   - automatic adding of users to predefined groups based on onepanel role
%%% @end
%%%-------------------------------------------------------------------
-module(user_logic_test_SUITE).
-author("<NAME>").

-include("registered_names.hrl").
-include("datastore/oz_datastore_models_def.hrl").
-include_lib("ctool/include/test/test_utils.hrl").
-include_lib("ctool/include/logging.hrl").
-include_lib("ctool/include/test/assertions.hrl").
-include_lib("ctool/include/test/performance.hrl").

%% API
-export([all/0, init_per_suite/1, end_per_suite/1, end_per_testcase/2]).
-export([
    set_space_name_mapping_test/1,
    clean_space_name_mapping_test/1,
    remove_space_test/1,
    basic_auth_login_test/1,
    automatic_group_membership_test/1,
    change_password_test/1
]).

all() ->
    ?ALL([
        set_space_name_mapping_test,
        clean_space_name_mapping_test,
        remove_space_test,
        basic_auth_login_test,
        automatic_group_membership_test,
        change_password_test
    ]).

%%%===================================================================
%%% Test functions
%%%===================================================================

set_space_name_mapping_test(Config) ->
    [Node | _] = Nodes = ?config(oz_worker_nodes, Config),

    {ok, UserId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_user(Config, #onedata_user{})
    ),

    SpaceName1 = <<"space_name">>,
    {ok, SpaceId1} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName1)),
    ?assertEqual(SpaceName1, get_space_name_mapping(Node, UserId, SpaceId1)),

    SpaceName2 = <<"different_space_name">>,
    {ok, SpaceId2} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName2)),
    ?assertEqual(SpaceName2, get_space_name_mapping(Node, UserId, SpaceId2)),

    SpaceName3 = <<"space_name">>,
    {ok, SpaceId3} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName3)),
    ?assertEqual(<<SpaceName3/binary, "#", SpaceId3:6/binary>>,
        get_space_name_mapping(Node, UserId, SpaceId3)),

    SpaceName4 = <<"space_name">>,
    SpaceId4 = <<SpaceId3:6/binary, "$random">>,
    space_save_mock(Nodes, SpaceId4),
    {ok, SpaceId4} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName4)),
    ?assertEqual(<<SpaceName4/binary, "#", SpaceId4:7/binary>>,
        get_space_name_mapping(Node, UserId, SpaceId4)),

    SpaceName5 = <<"modified_space_name">>,
    ?assertEqual(ok, oz_test_utils:modify_space(
        Config, SpaceId4, {user, UserId}, SpaceName5)),
    ?assertEqual(SpaceName5, get_space_name_mapping(Node, UserId, SpaceId4)).

clean_space_name_mapping_test(Config) ->
    [Node | _] = ?config(oz_worker_nodes, Config),

    SpaceName = <<"space_name">>,
    {ok, UserId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_user(Config, #onedata_user{})
    ),
    {ok, GroupId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_group(Config, UserId, <<"group">>)
    ),
    {ok, SpaceId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_space(Config, {group, GroupId}, SpaceName)
    ),
    ?assertMatch({ok, _},
        oz_test_utils:add_member_to_space(Config, {user, UserId}, SpaceId)),

    ?assertNot(clean_space_name_mapping(Node, UserId, SpaceId)),
    ?assertEqual(SpaceName, get_space_name_mapping(Node, UserId, SpaceId)),

    ?assert(oz_test_utils:leave_space(Config, {group, GroupId}, SpaceId)),
    ?assertNot(clean_space_name_mapping(Node, UserId, SpaceId)),
    ?assertEqual(SpaceName, get_space_name_mapping(Node, UserId, SpaceId)),

    ?assert(oz_test_utils:leave_space(Config, {user, UserId}, SpaceId)),
    ?assert(clean_space_name_mapping(Node, UserId, SpaceId)),
    ?assertMatch({ok, #document{value = #onedata_user{space_names = #{}}}},
        rpc:call(Node, onedata_user, get, [UserId])).

remove_space_test(Config) ->
    [Node | _] = ?config(oz_worker_nodes, Config),

    SpaceName = <<"space_name">>,
    {ok, UserId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_user(Config, #onedata_user{})
    ),
    {ok, SpaceId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_space(Config, {user, UserId}, SpaceName)
    ),

    ?assertEqual(SpaceName, get_space_name_mapping(Node, UserId, SpaceId)),
    oz_test_utils:remove_space(Config, SpaceId),
    ?assertMatch({ok, #document{value = #onedata_user{<commit_msg>VFS-2607 Adjust user logic tests to the fact that every user gets a default space<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME>
%%% @copyright (C): 2014 ACK CYFRONET AGH
%%% This software is released under the MIT license
%%% cited in 'LICENSE.txt'.
%%% @end
%%%-------------------------------------------------------------------
%%% @doc This file contains tests for user logic module concerning:
%%%   - personal space name mapping for users
%%%   - logging in via basic auth by interacting with onepanel
%%%   - automatic adding of users to predefined groups based on onepanel role
%%% @end
%%%-------------------------------------------------------------------
-module(user_logic_test_SUITE).
-author("<NAME>").

-include("registered_names.hrl").
-include("datastore/oz_datastore_models_def.hrl").
-include_lib("ctool/include/test/test_utils.hrl").
-include_lib("ctool/include/logging.hrl").
-include_lib("ctool/include/test/assertions.hrl").
-include_lib("ctool/include/test/performance.hrl").

%% API
-export([all/0, init_per_suite/1, end_per_suite/1, end_per_testcase/2]).
-export([
    set_space_name_mapping_test/1,
    clean_space_name_mapping_test/1,
    remove_space_test/1,
    basic_auth_login_test/1,
    automatic_group_membership_test/1,
    change_password_test/1
]).

all() ->
    ?ALL([
        set_space_name_mapping_test,
        clean_space_name_mapping_test,
        remove_space_test,
        basic_auth_login_test,
        automatic_group_membership_test,
        change_password_test
    ]).

%%%===================================================================
%%% Test functions
%%%===================================================================

set_space_name_mapping_test(Config) ->
    [Node | _] = Nodes = ?config(oz_worker_nodes, Config),

    {ok, UserId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_user(Config, #onedata_user{})
    ),

    SpaceName1 = <<"space_name">>,
    {ok, SpaceId1} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName1)),
    ?assertEqual(SpaceName1, get_space_name_mapping(Node, UserId, SpaceId1)),

    SpaceName2 = <<"different_space_name">>,
    {ok, SpaceId2} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName2)),
    ?assertEqual(SpaceName2, get_space_name_mapping(Node, UserId, SpaceId2)),

    SpaceName3 = <<"space_name">>,
    {ok, SpaceId3} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName3)),
    ?assertEqual(<<SpaceName3/binary, "#", SpaceId3:6/binary>>,
        get_space_name_mapping(Node, UserId, SpaceId3)),

    SpaceName4 = <<"space_name">>,
    SpaceId4 = <<SpaceId3:6/binary, "$random">>,
    space_save_mock(Nodes, SpaceId4),
    {ok, SpaceId4} = ?assertMatch({ok, _},
        oz_test_utils:create_space(Config, {user, UserId}, SpaceName4)),
    ?assertEqual(<<SpaceName4/binary, "#", SpaceId4:7/binary>>,
        get_space_name_mapping(Node, UserId, SpaceId4)),

    SpaceName5 = <<"modified_space_name">>,
    ?assertEqual(ok, oz_test_utils:modify_space(
        Config, SpaceId4, {user, UserId}, SpaceName5)),
    ?assertEqual(SpaceName5, get_space_name_mapping(Node, UserId, SpaceId4)).

clean_space_name_mapping_test(Config) ->
    [Node | _] = ?config(oz_worker_nodes, Config),

    SpaceName = <<"space_name">>,
    {ok, UserId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_user(Config, #onedata_user{})
    ),
    {ok, GroupId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_group(Config, UserId, <<"group">>)
    ),
    {ok, SpaceId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_space(Config, {group, GroupId}, SpaceName)
    ),
    ?assertMatch({ok, _},
        oz_test_utils:add_member_to_space(Config, {user, UserId}, SpaceId)),

    ?assertNot(clean_space_name_mapping(Node, UserId, SpaceId)),
    ?assertEqual(SpaceName, get_space_name_mapping(Node, UserId, SpaceId)),

    ?assert(oz_test_utils:leave_space(Config, {group, GroupId}, SpaceId)),
    ?assertNot(clean_space_name_mapping(Node, UserId, SpaceId)),
    ?assertEqual(SpaceName, get_space_name_mapping(Node, UserId, SpaceId)),

    ?assert(oz_test_utils:leave_space(Config, {user, UserId}, SpaceId)),
    ?assert(clean_space_name_mapping(Node, UserId, SpaceId)),
    ?assertMatch({ok, #document{value = #onedata_user{space_names = #{}}}},
        rpc:call(Node, onedata_user, get, [UserId])).

remove_space_test(Config) ->
    [Node | _] = ?config(oz_worker_nodes, Config),

    SpaceName = <<"space_name">>,
    {ok, UserId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_user(Config, #onedata_user{})
    ),
    % Every user gets his first space upon creation, account it
    {ok, #document{
        value = #onedata_user{
            spaces = [FirstSpace],
            space_names = #{FirstSpace := FirstSpaceName}
        }}} = oz_test_utils:get_user(Config, UserId),
    {ok, SpaceId} = ?assertMatch(
        {ok, _}, oz_test_utils:create_space(Config, {user, UserId}, SpaceName)
    ),

    ?assertEqual(SpaceName, get_space_name_mapping(Node, UserId, SpaceId)),
    ?assertEqual(FirstSpaceName,
        get_space_name_mapping(Node, UserId, FirstSpace)
    ),
    oz_test_utils:remove_space(Config, SpaceId),
    oz_test_utils:remove_space(Config, FirstSpaceName),
    ?assertMatch({ok, #document{value = #onedata_user{
{'fraction_non_alphanumeric': 0.20591361484201373, 'fraction_numerical': 0.00946951396270171, 'mean_word_length': 3.8206800186306475, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3619870', 'n_tokens_mistral': 3446, 'n_tokens_neox': 3379, 'n_words': 568}