Skip to content

Commit

Permalink
Push works
Browse files Browse the repository at this point in the history
  • Loading branch information
cheroliv committed Oct 21, 2024
1 parent 5145868 commit 32dda11
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
3 changes: 2 additions & 1 deletion api/src/test/kotlin/school/users/UserDaoTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class UserDaoTests {


@Test
fun `test findOneWithAuths with one query`(): Unit = runBlocking {
fun `test findOneWithAuths with one query using h2 database`(): Unit = runBlocking {
assertEquals(0, context.countUsers())
assertEquals(0, context.countUserAuthority())
(user to context).signup()
Expand Down Expand Up @@ -90,6 +90,7 @@ class UserDaoTests {
u.login,
u.password,
u.lang_key,
u.version,
GROUP_CONCAT(DISTINCT a.role) AS user_roles
FROM `user` as u
LEFT JOIN
Expand Down
34 changes: 34 additions & 0 deletions api/src/test/resources/database/h2database.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CREATE TABLE IF NOT EXISTS `user` (
`id` UUID default random_uuid() PRIMARY KEY,
`login` VARCHAR,
`password` VARCHAR,
`email` VARCHAR,
`lang_key` VARCHAR,
`version` bigint
);
CREATE UNIQUE INDEX IF NOT EXISTS `uniq_idx_user_login`
ON `user` (`login`);
CREATE UNIQUE INDEX IF NOT EXISTS `uniq_idx_user_email`
ON `user` (`email`);

CREATE TABLE IF NOT EXISTS `authority`(
`role` VARCHAR(50) PRIMARY KEY);
MERGE INTO `authority`
VALUES ('ADMIN'),
('USER'),
('ANONYMOUS');

CREATE TABLE IF NOT EXISTS `user_authority`(
`id` IDENTITY NOT NULL PRIMARY KEY,
`user_id` UUID,
`role` VARCHAR,
FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`role`) REFERENCES `authority` (`role`)
ON DELETE CASCADE
ON UPDATE CASCADE
);

CREATE UNIQUE INDEX IF NOT EXISTS `uniq_idx_user_authority`
ON `user_authority` (`role`, `user_id`);
59 changes: 59 additions & 0 deletions api/src/test/resources/database/h2playground.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
INSERT INTO `user` (login, password, email, lang_key, version)
VALUES ('user', 'user', '[email protected]', 'fr', '0'),
('admin', 'admin', '[email protected]', 'en', '0');

insert into `user_authority` (user_id, role)
select u.id, 'USER'
from `user` as u
where u.login = 'user';

insert into `user_authority` (user_id, role)
select u.id, 'USER'
from `user` as u
where u.login = 'admin';

insert into `user_authority` (user_id, role)
select u.id, 'ADMIN'
from `user` as u
where u.login = 'admin';

select count(*) from `USER`;

SELECT
u.id,
u.email,
u.login,
u.password,
u.lang_key,
u.version,
GROUP_CONCAT(DISTINCT a.role) AS user_roles
FROM `user` as u
LEFT JOIN
user_authority ua ON u.id = ua.user_id
LEFT JOIN
`authority` as a ON ua.role = a.role
WHERE
lower(u.email) = lower('user') OR lower(u.login) = lower('user')
GROUP BY
u.id, u.email, u.login;


SELECT
u.id,
u.email,
u.login,
u.password,
u.lang_key,
u.version,
GROUP_CONCAT(DISTINCT a.role) AS user_roles
FROM `user` as u
LEFT JOIN
user_authority ua ON u.id = ua.user_id
LEFT JOIN
`authority` as a ON ua.role = a.role
WHERE
lower(u.email) = lower('admin') OR lower(u.login) = lower('admin')
GROUP BY
u.id, u.email, u.login;


0 comments on commit 32dda11

Please sign in to comment.