From f3880091b77c19c87c93186f1e52c31c93bebbef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Wed, 16 Jun 2021 01:59:50 +0200
Subject: [PATCH] Podpora testovaciho schematu db

---
 .gitlab-ci.yml |  2 +-
 lib/CF.pm      | 12 +++++++-
 t/events.t     | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 2 deletions(-)
 create mode 100644 t/events.t

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7e7dbb6..849d5dc 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:19.03.12
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 2.2.0
+  IMAGE_VER: 2.3.0
 
 services:
   - docker:19.03.12-dind
diff --git a/lib/CF.pm b/lib/CF.pm
index 2e7a0e1..1fd445f 100644
--- a/lib/CF.pm
+++ b/lib/CF.pm
@@ -26,6 +26,11 @@ sub startup {
         ->username($cfg->{db_username})
         ->password($cfg->{db_password})
     ;
+    if ($cfg->{test}) {
+        $pg->search_path(['test']);
+        $pg->db->query('drop schema if exists test cascade');
+        $pg->db->query('create schema test');
+    }
     $pg->migrations->from_dir($self->home . '/sql');
     $pg->migrations->migrate();
     $self->helper( pg => sub { return $pg; } );
@@ -34,8 +39,13 @@ sub startup {
     my $schema = CF::Schema->connect({
         dsn      => $cfg->{db_dsn},
         user     => $cfg->{db_username},
-        password => $cfg->{db_password}
+        password => $cfg->{db_password},
     });
+
+    if ( $cfg->{test} ) {
+        $schema->storage->dbh->do("set search_path to test") ;
+    }
+
     $self->helper( schema => sub { return $schema; } );
 
     # Redis
diff --git a/t/events.t b/t/events.t
new file mode 100644
index 0000000..9a3bde1
--- /dev/null
+++ b/t/events.t
@@ -0,0 +1,81 @@
+use Mojo::Base -strict;
+
+use Test::More;
+use Test::Mojo;
+
+use constant SECRET => 'TEST';
+use constant USERS  => {
+    nobody => {
+      "sub"    => "00000000-0000-0000-0000-nobody",
+      "roles"  => [],
+      "name"   => "Testováci Nikdo",
+      "groups" => [],
+      "preferred_username" => "test.nobody",
+    },
+    organizer => {
+      "sub"    => "00000000-0000-0000-0000-organizer",
+      "roles"  => [ "organizer" ],
+      "name"   => "Testováci Pořadatel",
+      "groups" => [ "cen:f", ],
+      "preferred_username" => "test.organizer",
+    },
+};
+
+my $t = Test::Mojo->new('CF', {
+    test                 => 1,
+    test_auth_jwt_secret => SECRET,
+});
+
+# pokus najit uzivatele
+$t->get_ok('/api/sso/subjects?search=andrej.ramaseuski')->status_is(200)
+    ->json_is('/0/value' => 'andrej.ramaseuski')
+;
+
+# seznam udalosti pro neuautorizovaneho
+$t->get_ok('/api/events')->status_is(200);
+
+# Pokus pridat event neautorizovanym uzivatelem
+$t->post_ok('/api/events')->status_is(401);
+
+# Pokus pridat event neopravnenym uzivatelem
+$t->post_ok('/api/events', {
+    Authorization => auth_header(USERS->{nobody})
+})->status_is(401);
+
+# Pokus pridat event bez dat
+$t->post_ok('/api/events' => {
+    Authorization => auth_header(USERS->{organizer})
+})->status_is(400);
+
+# pokus pridat event bez validnich dat
+$t->post_ok('/api/events' => {
+    Authorization => auth_header(USERS->{organizer})
+},
+json => {
+})->status_is(400);
+
+# pokus pridat event
+$t->post_ok('/api/events' => {
+    Authorization => auth_header(USERS->{organizer})
+},
+json => {
+  "type"        => 1,
+  "is_opened"   => 1,
+  "start"       => "2021-07-18 10:00",
+  "finish"      => "2021-07-18 18:00",
+  "name"        => "SchĹŻze KS PardubickĂ˝ kraj",
+  "description" => "TEST 8968714465",
+  "organizer"   => "PKS PardubickĂ˝ kraj"
+})->status_is(201);
+
+$t->app->pg->db->query('drop schema if exists test cascade');
+done_testing();
+
+sub auth_header {
+    my $claims = shift;
+    return 'Bearer ' . Mojo::JWT->new(
+        claims => $claims, secret => SECRET
+    )->encode();
+}
+
+
-- 
GitLab