1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/*
 * Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPoolOptions;
use sqlx::types::time::OffsetDateTime;
use sqlx::ConnectOptions;
use sqlx::PgPool;
use tracing::error;
use uuid::Uuid;

use crate::errors::*;

/// Connect to databse
pub enum ConnectionOptions {
    /// fresh connection
    Fresh(Fresh),
    /// existing connection
    Existing(Conn),
}

/// Use an existing database pool
pub struct Conn(pub PgPool);

pub struct Fresh {
    pub pool_options: PgPoolOptions,
    pub disable_logging: bool,
    pub url: String,
}

impl ConnectionOptions {
    async fn connect(self) -> ServiceResult<Database> {
        let pool = match self {
            Self::Fresh(fresh) => {
                let mut connect_options =
                    sqlx::postgres::PgConnectOptions::from_str(&fresh.url).unwrap();
                if fresh.disable_logging {
                    connect_options.disable_statement_logging();
                }
                sqlx::postgres::PgConnectOptions::from_str(&fresh.url)
                    .unwrap()
                    .disable_statement_logging();
                fresh
                    .pool_options
                    .connect_with(connect_options)
                    .await
                    .unwrap()
                //.map_err(|e| ServiceError::ServiceError(Box::new(e)))?
            }

            Self::Existing(conn) => conn.0,
        };
        Ok(Database { pool })
    }
}

#[derive(Clone)]
pub struct Database {
    pub pool: PgPool,
}

impl Database {
    pub async fn migrate(&self) -> ServiceResult<()> {
        sqlx::migrate!("./migrations/")
            .run(&self.pool)
            .await
            .unwrap();
        //.map_err(|e| ServiceError::ServiceError(Box::new(e)))?;
        self.create_event_type().await?;
        Ok(())
    }

    pub async fn ping(&self) -> bool {
        use sqlx::Connection;

        if let Ok(mut con) = self.pool.acquire().await {
            con.ping().await.is_ok()
        } else {
            false
        }
    }

    /// register a new user
    pub async fn register(&self, p: &Register<'_>) -> ServiceResult<()> {
        sqlx::query!(
            "INSERT INTO librepages_users
            (name , password, email) VALUES ($1, $2, $3)",
            &p.username,
            &p.hash,
            &p.email,
        )
        .execute(&self.pool)
        .await
        .map_err(map_register_err)?;
        Ok(())
    }

    /// delete a user
    pub async fn delete_user(&self, username: &str) -> ServiceResult<()> {
        sqlx::query!("DELETE FROM librepages_users WHERE name = ($1)", username)
            .execute(&self.pool)
            .await
            .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
        Ok(())
    }

    /// check if username exists
    pub async fn username_exists(&self, username: &str) -> ServiceResult<bool> {
        let res = sqlx::query!(
            "SELECT EXISTS (SELECT 1 from librepages_users WHERE name = $1)",
            username,
        )
        .fetch_one(&self.pool)
        .await
        .map_err(map_register_err)?;

        let mut resp = false;
        if let Some(x) = res.exists {
            resp = x;
        }

        Ok(resp)
    }

    /// get user email
    pub async fn get_email(&self, username: &str) -> ServiceResult<String> {
        struct Email {
            email: String,
        }

        let res = sqlx::query_as!(
            Email,
            "SELECT email FROM librepages_users WHERE name = $1",
            username
        )
        .fetch_one(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;
        Ok(res.email)
    }

    /// check if email exists
    pub async fn email_exists(&self, email: &str) -> ServiceResult<bool> {
        let res = sqlx::query!(
            "SELECT EXISTS (SELECT 1 from librepages_users WHERE email = $1)",
            email
        )
        .fetch_one(&self.pool)
        .await
        .map_err(map_register_err)?;

        let mut resp = false;
        if let Some(x) = res.exists {
            resp = x;
        }

        Ok(resp)
    }

    /// update a user's email
    pub async fn update_email(&self, p: &UpdateEmail<'_>) -> ServiceResult<()> {
        sqlx::query!(
            "UPDATE librepages_users set email = $1
            WHERE name = $2",
            &p.new_email,
            &p.username,
        )
        .execute(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        Ok(())
    }

    /// get a user's password
    pub async fn get_password(&self, l: &Login<'_>) -> ServiceResult<NameHash> {
        struct Password {
            name: String,
            password: String,
        }

        let rec = match l {
            Login::Username(u) => sqlx::query_as!(
                Password,
                r#"SELECT name, password  FROM librepages_users WHERE name = ($1)"#,
                u,
            )
            .fetch_one(&self.pool)
            .await
            .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?,
            Login::Email(e) => sqlx::query_as!(
                Password,
                r#"SELECT name, password  FROM librepages_users WHERE email = ($1)"#,
                e,
            )
            .fetch_one(&self.pool)
            .await
            .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?,
        };

        let res = NameHash {
            hash: rec.password,
            username: rec.name,
        };

        Ok(res)
    }

    /// update user's password
    pub async fn update_password(&self, p: &NameHash) -> ServiceResult<()> {
        sqlx::query!(
            "UPDATE librepages_users set password = $1
            WHERE name = $2",
            &p.hash,
            &p.username,
        )
        .execute(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        Ok(())
    }

    /// update username
    pub async fn update_username(&self, current: &str, new: &str) -> ServiceResult<()> {
        sqlx::query!(
            "UPDATE librepages_users set name = $1
            WHERE name = $2",
            new,
            current,
        )
        .execute(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        Ok(())
    }

    pub async fn add_site(&self, msg: &Site) -> ServiceResult<()> {
        sqlx::query!(
            "
            INSERT INTO librepages_sites
                (site_secret, repo_url, branch, hostname, owned_by)
            VALUES ($1, $2, $3, $4, ( SELECT ID FROM librepages_users WHERE name = $5 ));
            ",
            msg.site_secret,
            msg.repo_url,
            msg.branch,
            msg.hostname,
            msg.owner,
        )
        .execute(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        Ok(())
    }

    pub async fn get_site_from_secret(&self, site_secret: &str) -> ServiceResult<Site> {
        struct S {
            repo_url: String,
            branch: String,
            hostname: String,
            owned_by: i32,
        }

        let site = sqlx::query_as!(
            S,
            "SELECT repo_url, branch, hostname, owned_by
            FROM librepages_sites
            WHERE site_secret = $1
            ",
            site_secret,
        )
        .fetch_one(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;

        struct Owner {
            name: String,
        }
        let owner = sqlx::query_as!(
            Owner,
            "SELECT name FROM librepages_users WHERE ID = $1",
            site.owned_by
        )
        .fetch_one(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;

        let site = Site {
            site_secret: site_secret.to_owned(),
            branch: site.branch,
            hostname: site.hostname,
            owner: owner.name,
            repo_url: site.repo_url,
        };

        Ok(site)
    }

    pub async fn get_site(&self, owner: &str, hostname: &str) -> ServiceResult<Site> {
        let site = sqlx::query_as!(
            InnerSite,
            "SELECT site_secret, repo_url, branch, hostname
            FROM librepages_sites
            WHERE owned_by = (SELECT ID FROM librepages_users WHERE name = $1 )
            AND hostname = $2;
            ",
            owner,
            hostname
        )
        .fetch_one(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;

        let res = site.to_site(owner.into());

        Ok(res)
    }

    pub async fn list_all_sites(&self, owner: &str) -> ServiceResult<Vec<Site>> {
        let mut sites = sqlx::query_as!(
            InnerSite,
            "SELECT site_secret, repo_url, branch, hostname
            FROM librepages_sites
            WHERE owned_by = (SELECT ID FROM librepages_users WHERE name = $1 );
            ",
            owner,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        let res = sites.drain(0..).map(|s| s.to_site(owner.into())).collect();

        Ok(res)
    }

    pub async fn delete_site(&self, owner: &str, hostname: &str) -> ServiceResult<()> {
        sqlx::query!(
            "DELETE FROM librepages_sites
            WHERE hostname = ($1)
            AND owned_by = ( SELECT ID FROM librepages_users WHERE name = $2);
            ",
            hostname,
            owner
        )
        .execute(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::WebsiteNotFound))?;
        Ok(())
    }

    /// check if hostname exists
    pub async fn hostname_exists(&self, hostname: &str) -> ServiceResult<bool> {
        let res = sqlx::query!(
            "SELECT EXISTS (SELECT 1 from librepages_sites WHERE hostname = $1)",
            hostname,
        )
        .fetch_one(&self.pool)
        .await
        .map_err(map_register_err)?;

        let mut resp = false;
        if let Some(x) = res.exists {
            resp = x;
        }

        Ok(resp)
    }

    /// check if event type exists
    async fn event_type_exists(&self, event: &Event) -> ServiceResult<bool> {
        let res = sqlx::query!(
            "SELECT EXISTS (SELECT 1 from librepages_deploy_event_type WHERE name = $1)",
            event.name,
        )
        .fetch_one(&self.pool)
        .await
        .map_err(map_register_err)?;

        let mut resp = false;
        if let Some(x) = res.exists {
            resp = x;
        }

        Ok(resp)
    }

    async fn create_event_type(&self) -> ServiceResult<()> {
        for e in EVENTS {
            if !self.event_type_exists(&e).await? {
                sqlx::query!(
                    "INSERT INTO librepages_deploy_event_type
                    (name) VALUES ($1) ON CONFLICT (name) DO NOTHING;",
                    e.name
                )
                .execute(&self.pool)
                .await
                .map_err(map_register_err)?;
            }
        }
        Ok(())
    }

    pub async fn log_event(&self, hostname: &str, event: &Event) -> ServiceResult<Uuid> {
        let now = now_unix_time_stamp();
        let uuid = Uuid::new_v4();

        sqlx::query!(
            "INSERT INTO librepages_site_deploy_events
            (event_type, time, site, pub_id) VALUES (
                (SELECT iD from librepages_deploy_event_type WHERE name = $1),
                $2,
                (SELECT ID from librepages_sites WHERE hostname = $3),
                $4
            );
            ",
            event.name,
            &now,
            hostname,
            uuid,
        )
        .execute(&self.pool)
        .await
        .map_err(map_register_err)?;
        Ok(uuid)
    }

    pub async fn get_event(
        &self,
        hostname: &str,
        event_id: &Uuid,
    ) -> ServiceResult<LibrePagesEvent> {
        let event = sqlx::query_as!(
            InnerLibrepagesEvent,
            "SELECT
                    librepages_deploy_event_type.name,
                    librepages_site_deploy_events.time,
                    librepages_site_deploy_events.pub_id
                FROM
                    librepages_site_deploy_events
                INNER JOIN librepages_deploy_event_type ON
                    librepages_deploy_event_type.ID = librepages_site_deploy_events.event_type
                WHERE
                    librepages_site_deploy_events.site = (
                        SELECT ID FROM librepages_sites WHERE hostname = $1
                    )
                AND
                    librepages_site_deploy_events.pub_id = $2
                ",
            hostname,
            event_id,
        )
        .fetch_one(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        Ok(LibrePagesEvent {
            id: event.pub_id,
            time: event.time,
            event_type: Event::from_str(&event.name).unwrap(),
            site: hostname.to_owned(),
        })
    }

    pub async fn list_all_site_events(
        &self,
        hostname: &str,
    ) -> ServiceResult<Vec<LibrePagesEvent>> {
        let mut inner_events = sqlx::query_as!(
            InnerLibrepagesEvent,
            "SELECT
                    librepages_deploy_event_type.name,
                    librepages_site_deploy_events.time,
                    librepages_site_deploy_events.pub_id
                FROM
                    librepages_site_deploy_events
                INNER JOIN librepages_deploy_event_type ON
                    librepages_deploy_event_type.ID = librepages_site_deploy_events.event_type
                WHERE
                    librepages_site_deploy_events.site = (
                        SELECT ID FROM librepages_sites WHERE hostname = $1
                    );
                ",
            hostname,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(|e| map_row_not_found_err(e, ServiceError::AccountNotFound))?;

        let mut events = Vec::with_capacity(inner_events.len());

        for e in inner_events.drain(0..) {
            events.push(LibrePagesEvent {
                id: e.pub_id,
                time: e.time,
                event_type: Event::from_str(&e.name).unwrap(),
                site: hostname.to_owned(),
            })
        }
        Ok(events)
    }
}
struct InnerSite {
    site_secret: String,
    repo_url: String,
    branch: String,
    hostname: String,
}

impl InnerSite {
    fn to_site(self, owner: String) -> Site {
        Site {
            site_secret: self.site_secret,
            repo_url: self.repo_url,
            branch: self.branch,
            hostname: self.hostname,
            owner,
        }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
/// Data required to add a new site
pub struct Site {
    pub site_secret: String,
    pub repo_url: String,
    pub branch: String,
    pub hostname: String,
    pub owner: String,
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
/// Data required to register a new user
pub struct Register<'a> {
    /// username of new user
    pub username: &'a str,
    /// hashed password of new use
    pub hash: &'a str,
    /// Optionally, email of new use
    pub email: &'a str,
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
/// data required to update them email of a user
pub struct UpdateEmail<'a> {
    /// username of the user
    pub username: &'a str,
    /// new email address of the user
    pub new_email: &'a str,
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
/// types of credentials used as identifiers during login
pub enum Login<'a> {
    /// username as login
    Username(&'a str),
    /// email as login
    Email(&'a str),
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
/// type encapsulating username and hashed password of a user
pub struct NameHash {
    /// username
    pub username: String,
    /// hashed password
    pub hash: String,
}

#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
pub struct Event {
    pub name: &'static str,
}

impl Event {
    const fn new(name: &'static str) -> Self {
        Self { name }
    }

    pub fn from_str(name: &str) -> Option<Event> {
        EVENTS.into_iter().find(|e| e.name == name)
    }
}
pub const EVENT_TYPE_CREATE: Event = Event::new("site.event.create");
pub const EVENT_TYPE_UPDATE: Event = Event::new("site.event.update");
pub const EVENT_TYPE_DELETE: Event = Event::new("site.event.delete");

pub const EVENTS: [Event; 3] = [EVENT_TYPE_CREATE, EVENT_TYPE_DELETE, EVENT_TYPE_UPDATE];

struct InnerLibrepagesEvent {
    name: String,
    time: OffsetDateTime,
    pub_id: Uuid,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LibrePagesEvent {
    pub event_type: Event,
    pub time: OffsetDateTime,
    pub site: String,
    pub id: Uuid,
}

fn now_unix_time_stamp() -> OffsetDateTime {
    OffsetDateTime::now_utc()
}

pub async fn get_db(settings: &crate::settings::Settings) -> Database {
    let pool_options = PgPoolOptions::new().max_connections(settings.database.pool);
    ConnectionOptions::Fresh(Fresh {
        pool_options,
        url: settings.database.url.clone(),
        disable_logging: !settings.debug,
    })
    .connect()
    .await
    .unwrap()
}

/// map custom row not found error to DB error
pub fn map_row_not_found_err(e: sqlx::Error, row_not_found: ServiceError) -> ServiceError {
    if let sqlx::Error::RowNotFound = e {
        row_not_found
    } else {
        map_register_err(e)
    }
}

/// map postgres errors to [ServiceError](ServiceError) types
fn map_register_err(e: sqlx::Error) -> ServiceError {
    use sqlx::Error;
    use std::borrow::Cow;

    if let Error::Database(err) = e {
        if err.code() == Some(Cow::from("23505")) {
            let msg = err.message();
            println!("{}", msg);
            if msg.contains("librepages_users_name_key") {
                ServiceError::UsernameTaken
            } else if msg.contains("librepages_users_email_key") {
                ServiceError::EmailTaken
            } else {
                error!("{}", msg);
                ServiceError::InternalServerError
            }
        } else {
            ServiceError::InternalServerError
        }
    } else {
        ServiceError::InternalServerError
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;
    use crate::settings::Settings;

    #[test]
    fn event_names_are_unique() {
        let mut uniq = HashSet::new();
        assert!(EVENTS.into_iter().all(move |x| uniq.insert(x.name)));
    }

    #[actix_rt::test]
    async fn db_works() {
        let settings = Settings::new().unwrap();
        let pool_options = PgPoolOptions::new().max_connections(1);
        let db = ConnectionOptions::Fresh(Fresh {
            pool_options,
            url: settings.database.url.clone(),
            disable_logging: !settings.debug,
        })
        .connect()
        .await
        .unwrap();
        assert!(db.ping().await);

        const EMAIL: &str = "postgresuser@foo.com";
        const EMAIL2: &str = "postgresuser2@foo.com";
        const NAME: &str = "postgresuser";
        const PASSWORD: &str = "pasdfasdfasdfadf";

        db.migrate().await.unwrap();
        let p = super::Register {
            username: NAME,
            email: EMAIL,
            hash: PASSWORD,
        };

        if db.username_exists(p.username).await.unwrap() {
            db.delete_user(p.username).await.unwrap();
            assert!(
                !db.username_exists(p.username).await.unwrap(),
                "user is deleted so username shouldn't exist"
            );
        }

        db.register(&p).await.unwrap();

        assert!(matches!(
            db.register(&p).await,
            Err(ServiceError::UsernameTaken)
        ));

        // testing get_password

        // with username
        let name_hash = db.get_password(&Login::Username(p.username)).await.unwrap();
        assert_eq!(name_hash.hash, p.hash, "user password matches");

        assert_eq!(name_hash.username, p.username, "username matches");

        // with email
        let mut name_hash = db.get_password(&Login::Email(p.email)).await.unwrap();
        assert_eq!(name_hash.hash, p.hash, "user password matches");
        assert_eq!(name_hash.username, p.username, "username matches");

        // testing get_email
        assert_eq!(db.get_email(p.username).await.unwrap(), p.email);

        // testing email exists
        assert!(
            db.email_exists(p.email).await.unwrap(),
            "user is registered so email should exist"
        );
        assert!(
            db.username_exists(p.username).await.unwrap(),
            "user is registered so username should exist"
        );

        // update password test. setting password = username
        name_hash.hash = name_hash.username.clone();
        db.update_password(&name_hash).await.unwrap();

        let name_hash = db.get_password(&Login::Username(p.username)).await.unwrap();
        assert_eq!(
            name_hash.hash, p.username,
            "user password matches with changed value"
        );
        assert_eq!(name_hash.username, p.username, "username matches");

        // update username to p.email
        assert!(
            !db.username_exists(p.email).await.unwrap(),
            "user with p.email doesn't exist. pre-check to update username to p.email"
        );
        db.update_username(p.username, p.email).await.unwrap();
        assert!(
            db.username_exists(p.email).await.unwrap(),
            "user with p.email exist post-update"
        );

        // testing update email
        let update_email = UpdateEmail {
            username: p.username,
            new_email: EMAIL2,
        };
        db.update_email(&update_email).await.unwrap();
        println!(
            "null user email: {}",
            db.email_exists(p.email).await.unwrap()
        );
        assert!(
            db.email_exists(p.email).await.unwrap(),
            "user was with empty email but email is set; so email should exist"
        );

        // deleting user
        db.delete_user(p.email).await.unwrap();
        assert!(
            !db.username_exists(p.email).await.unwrap(),
            "user is deleted so username shouldn't exist"
        );
    }

    #[actix_rt::test]
    pub async fn test_db_sites() {
        let settings = Settings::new().unwrap();
        let pool_options = PgPoolOptions::new().max_connections(1);
        let db = ConnectionOptions::Fresh(Fresh {
            pool_options,
            url: settings.database.url.clone(),
            disable_logging: !settings.debug,
        })
        .connect()
        .await
        .unwrap();
        assert!(db.ping().await);

        const EMAIL: &str = "postgresdbsiteuser@foo.com";
        const NAME: &str = "postgresdbsiteuser";
        const PASSWORD: &str = "pasdfasdfasdfadf";

        db.migrate().await.unwrap();

        // check if events are created
        for e in EVENTS {
            println!("Testing event type exists {}", e.name);
            assert!(db.event_type_exists(&e).await.unwrap());
        }

        let p = super::Register {
            username: NAME,
            email: EMAIL,
            hash: PASSWORD,
        };

        if db.username_exists(p.username).await.unwrap() {
            db.delete_user(p.username).await.unwrap();
            assert!(
                !db.username_exists(p.username).await.unwrap(),
                "user is deleted so username shouldn't exist"
            );
        }

        db.register(&p).await.unwrap();

        let site = Site {
            site_secret: "foobar".into(),
            repo_url: "https://git.batsense.net/LibrePages/librepages.git".into(),
            branch: "librepages".into(),
            hostname: "db_works.tests.librepages.librepages.org".into(),
            owner: p.username.into(),
        };

        // test if hostname exists. Should be false
        assert!(!db.hostname_exists(&site.hostname).await.unwrap());

        // testing adding site
        db.add_site(&site).await.unwrap();

        // test if hostname exists. Should be true
        assert!(db.hostname_exists(&site.hostname).await.unwrap());

        // get site
        let db_site = db.get_site(p.username, &site.hostname).await.unwrap();
        assert_eq!(db_site, site);

        // get site by secret
        assert_eq!(
            db_site,
            db.get_site_from_secret(&site.site_secret).await.unwrap()
        );

        // list all sites owned by user
        let db_sites = db.list_all_sites(p.username).await.unwrap();
        assert_eq!(db_sites.len(), 1);
        assert_eq!(db_sites, vec![site.clone()]);

        // add event to site
        let event_id = db
            .log_event(&site.hostname, &EVENT_TYPE_CREATE)
            .await
            .unwrap();
        let event = db.get_event(&site.hostname, &event_id).await.unwrap();
        assert_eq!(event.id, event_id);
        assert_eq!(event.event_type, EVENT_TYPE_CREATE);
        assert_eq!(event.site, site.hostname);

        assert_eq!(
            db.list_all_site_events(&site.hostname).await.unwrap(),
            vec![event]
        );

        // delete site
        db.delete_site(p.username, &site.hostname).await.unwrap();

        // test if hostname exists. Should be false
        assert!(!db.hostname_exists(&site.hostname).await.unwrap());
    }
}