Skip to content

Commit

Permalink
Apply review - Add tests for domain password management in mongoose_d…
Browse files Browse the repository at this point in the history
…omain_admin

Minor changes:
- rename "domain_admin" table to "domain_admins",
- fix password update operation.
  • Loading branch information
Premwoik committed Apr 29, 2022
1 parent a766346 commit 2bf7c82
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
58 changes: 58 additions & 0 deletions big_tests/tests/service_domain_db_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ db_cases() -> [
db_cannot_enable_domain_with_unknown_host_type,
db_cannot_disable_domain_with_unknown_host_type,
db_domains_with_unknown_host_type_are_ignored_by_core,
db_can_insert_update_delete_dynamic_domain_password,
db_can_insert_update_delete_static_domain_password,
db_cannot_set_password_for_unknown_domain,
db_can_check_domain_password,
db_cannot_check_password_for_unknown_domain,
db_deleting_domain_deletes_domain_admin,
sql_select_from,
sql_find_gaps_between,
db_records_are_restored_on_mim_restart,
Expand Down Expand Up @@ -451,6 +457,46 @@ db_domains_with_unknown_host_type_are_ignored_by_core(_) ->
{ok, <<"type1">>} = get_host_type(mim(), <<"example.org">>), %% Counter-case
{error, not_found} = get_host_type(mim(), <<"example.com">>).

db_can_insert_update_delete_dynamic_domain_password(_) ->
Domain = <<"password-example.com">>,
ok = insert_domain(mim(), Domain, <<"type1">>),
sync(),
ok = set_domain_password(mim(), Domain, <<"rocky1">>),
ok = check_domain_password(mim(), Domain, <<"rocky1">>),
ok = set_domain_password(mim(), Domain, <<"rocky2">>),
ok = check_domain_password(mim(), Domain, <<"rocky2">>),
ok = delete_domain_password(mim(), Domain),
{error, not_found} = select_domain_admin(mim(), Domain).

db_can_insert_update_delete_static_domain_password(_) ->
StaticDomain = <<"example.cfg">>,
ok = set_domain_password(mim(), StaticDomain, <<"rocky1">>),
ok = check_domain_password(mim(), StaticDomain, <<"rocky1">>),
ok = set_domain_password(mim(), StaticDomain, <<"rocky2">>),
ok = check_domain_password(mim(), StaticDomain, <<"rocky2">>),
ok = delete_domain_password(mim(), StaticDomain),
{error, not_found} = select_domain_admin(mim(), StaticDomain).

db_cannot_set_password_for_unknown_domain(_) ->
{error, not_found} = set_domain_password(mim(), <<"unknown_domain">>, <<>>).

db_can_check_domain_password(_) ->
StaticDomain = <<"example.cfg">>,
ok = set_domain_password(mim(), StaticDomain, <<"myrock">>),
ok = check_domain_password(mim(), StaticDomain, <<"myrock">>),
{error, wrong_password} = check_domain_password(mim(), StaticDomain, <<"wrongrock">>).

db_cannot_check_password_for_unknown_domain(_) ->
{error, not_found} = check_domain_password(mim(), <<"unknown_domain">>, <<>>).

db_deleting_domain_deletes_domain_admin(_) ->
Domain = <<"password-del-example.db">>,
ok = insert_domain(mim(), Domain, <<"type1">>),
sync(),
ok = set_domain_password(mim(), Domain, <<"deleteme">>),
ok = delete_domain(mim(), Domain, <<"type1">>),
{error, not_found} = select_domain_admin(mim(), Domain).

sql_select_from(_) ->
ok = insert_domain(mim(), <<"example.db">>, <<"type1">>),
[{_, <<"example.db">>, <<"type1">>}] =
Expand Down Expand Up @@ -1065,6 +1111,18 @@ delete_domain(Node, Domain, HostType) ->
select_domain(Node, Domain) ->
rpc(Node, mongoose_domain_sql, select_domain, [Domain]).

check_domain_password(Node, Domain, Password) ->
rpc(Node, mongoose_domain_api, check_domain_password, [Domain, Password]).

set_domain_password(Node, Domain, Password) ->
rpc(Node, mongoose_domain_api, set_domain_password, [Domain, Password]).

delete_domain_password(Node, Domain) ->
rpc(Node, mongoose_domain_api, delete_domain_password, [Domain]).

select_domain_admin(Node, Domain) ->
rpc(Node, mongoose_domain_sql, select_domain_admin, [Domain]).

insert_full_event(Node, EventId, Domain) ->
rpc(Node, mongoose_domain_sql, insert_full_event, [EventId, Domain]).

Expand Down
2 changes: 1 addition & 1 deletion priv/mssql2012.sql
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ CREATE TABLE offline_markers (

CREATE INDEX i_offline_markers ON offline_markers(jid);

CREATE TABLE domain_admin(
CREATE TABLE domain_admins(
domain VARCHAR(250) NOT NULL PRIMARY KEY,
password VARCHAR(250) NOT NULL
);
Expand Down
2 changes: 1 addition & 1 deletion priv/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ CREATE TABLE offline_markers (

CREATE INDEX i_offline_markers ON offline_markers(jid);

CREATE TABLE domain_admin(
CREATE TABLE domain_admins(
domain VARCHAR(250) NOT NULL,
password VARCHAR(250) NOT NULL,
PRIMARY KEY(domain)
Expand Down
2 changes: 1 addition & 1 deletion priv/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ CREATE TABLE offline_markers (

CREATE INDEX i_offline_markers ON offline_markers(jid);

CREATE TABLE domain_admin(
CREATE TABLE domain_admins(
domain VARCHAR(250) NOT NULL,
password VARCHAR(250) NOT NULL,
PRIMARY KEY(domain)
Expand Down
23 changes: 13 additions & 10 deletions src/domain/mongoose_domain_sql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,23 @@ start(#{db_pool := Pool}) ->
" WHERE domain_events.id >= ? AND domain_events.id <= ? "
" ORDER BY domain_events.id ">>),
%% Admins
prepare(domain_insert_admin, domain_admin, [domain, password],
<<"INSERT INTO domain_admin (domain, password) VALUES (?, ?)">>),
prepare(domain_update_admin, domain_admin, [password, domain],
<<"UPDATE domain_admin"
prepare(domain_insert_admin, domain_admins, [domain, password],
<<"INSERT INTO domain_admins (domain, password) VALUES (?, ?)">>),
prepare(domain_update_admin, domain_admins, [password, domain],
<<"UPDATE domain_admins"
" SET password = ? "
" WHERE domain = ?">>),
prepare(domain_delete_admin, domain_admin, [domain],
<<"DELETE FROM domain_admin WHERE domain = ?">>),
prepare(domain_select_admin, domain_admin, [domain],
prepare(domain_delete_admin, domain_admins, [domain],
<<"DELETE FROM domain_admins WHERE domain = ?">>),
prepare(domain_select_admin, domain_admins, [domain],
<<"SELECT domain, password"
" FROM domain_admin WHERE domain = ?">>),
" FROM domain_admins WHERE domain = ?">>),
ok.

prepare_test_queries(Pool) ->
True = sql_true(Pool),
prepare(domain_erase_admins, domain_admins, [],
<<"DELETE FROM domain_admins">>),
prepare(domain_erase_settings, domain_settings, [],
<<"DELETE FROM domain_settings">>),
prepare(domain_erase_events, domain_events, [],
Expand Down Expand Up @@ -200,7 +202,7 @@ insert_domain_admin(Pool, Domain, Password) ->
execute_successfully(Pool, domain_insert_admin, [Domain, Password]).

update_domain_admin(Pool, Domain, Password) ->
execute_successfully(Pool, domain_update_admin, [Domain, Password]).
execute_successfully(Pool, domain_update_admin, [Password, Domain]).

delete_domain_admin(Pool, Domain) ->
execute_successfully(Pool, domain_delete_admin, [Domain]).
Expand Down Expand Up @@ -287,7 +289,8 @@ insert_full_event_mssql(EventId, Domain) ->

erase_database(Pool) ->
execute_successfully(Pool, domain_erase_events, []),
execute_successfully(Pool, domain_erase_settings, []).
execute_successfully(Pool, domain_erase_settings, []),
execute_successfully(Pool, domain_erase_admins, []).

insert_domain_settings_without_event(Domain, HostType) ->
Pool = get_db_pool(),
Expand Down

0 comments on commit 2bf7c82

Please sign in to comment.