aboutsummaryrefslogtreecommitdiffstats
path: root/tests/apitests.rs
blob: e0803a8d8909b516b1fcab63fe9d57dd4aad4cbf (plain) (blame)
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
/*
    Social program for Ramaskrik.
    Copyright (C) 2019  Harald Eilertsen <haraldei@anduin.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 ramaskrik;
use ramaskrik::models::{Film, NewFilm, Room, Screening};

use lazy_static::lazy_static;
use rocket::http::ContentType;
use rocket_contrib::databases::diesel::prelude::*;
use serde_json;

lazy_static! {
    static ref MUTEX: std::sync::Mutex<()> = std::sync::Mutex::default();
}

pub fn server_with_db<TestFn>(f: TestFn)
    where
        TestFn: Fn(rocket::Rocket, ramaskrik::db::Connection)
{
    let _lock = MUTEX.lock();

    let db_url = dotenv::var("TEST_DATABASE_URL")
        .map_err(|_| "No database! Set TEST_DATABASE_URL env var and try again.").unwrap();

    let server = ramaskrik::build_rocket(&db_url).unwrap();
    let db = ramaskrik::db::Connection::get_one(&server).expect("Could not get db connection");

    load_fixtures(&db);

    f(server, db);
}

fn load_default_films(db: &ramaskrik::db::Connection) {
    use ramaskrik::schema::films::dsl::*;

    let new_films = vec![
        NewFilm { title: "Hellraiser", url: Some("https://www.imdb.com/title/tt0093177") },
        NewFilm { title: "Huset", url: Some("https://www.imdb.com/title/tt3425402") },
        NewFilm { title: "Skuld", url: Some("https://www.imdb.com/title/tt4344456") }];

    diesel::insert_into(films)
        .values(&new_films)
        .execute(&**db)
        .unwrap();
}

fn load_default_rooms(db: &ramaskrik::db::Connection) {
    use ramaskrik::schema::rooms::dsl::*;

    let new_rooms = vec![
        name.eq("Main room"),
        name.eq("Small room"),
        name.eq("Neverland")];

    diesel::insert_into(rooms)
        .values(&new_rooms)
        .execute(&**db)
        .unwrap();
}

fn load_fixtures(db: &ramaskrik::db::Connection) {
    diesel::dsl::sql_query("TRUNCATE TABLE rooms, screenings, films").execute(&**db).unwrap();
    load_default_rooms(&db);
    load_default_films(&db);
}

#[test]
fn getting_rooms_from_api() {
    server_with_db(|server, _| {
        let client = rocket::local::Client::new(server).unwrap();
        let mut response = client.get("/rooms").dispatch();
        assert_eq!(response.content_type(), Some(ContentType::JSON));

        let fetched_rooms: Vec<Room> =
            serde_json::from_str(&response.body_string().unwrap()).unwrap();

        assert_eq!(fetched_rooms.len(), 3);

        let room_names: Vec<&str> = fetched_rooms.iter().map(|room| room.name.as_str()).collect();
        assert!(room_names.contains(&"Main room"));
        assert!(room_names.contains(&"Small room"));
        assert!(room_names.contains(&"Neverland"));
    })
}

#[test]
fn getting_films_from_api() {
    server_with_db(|server, _| {
        let client = rocket::local::Client::new(server).unwrap();
        let mut response = client.get("/films").dispatch();
        assert_eq!(response.content_type(), Some(ContentType::JSON));

        let fetched_films: Vec<Film> =
            serde_json::from_str(&response.body_string().unwrap()).unwrap();

        assert_eq!(fetched_films.len(), 3);

        let film_names: Vec<&str> = fetched_films.iter().map(|film| film.title.as_str()).collect();
        assert!(film_names.contains(&"Hellraiser"));
        assert!(film_names.contains(&"Huset"));
        assert!(film_names.contains(&"Skuld"));
    });
}

#[test]
fn getting_screenings_from_api() {
    server_with_db(|server, db| {
        let r = db.get_room_by_name("Main room").unwrap();
        let f = db.get_film_by_title("Hellraiser").unwrap();

        db.create_screening(&r, &f,
            chrono::NaiveDate::from_ymd(2019, 10, 21),
            chrono::NaiveTime::from_hms(18, 00, 00),
            chrono::NaiveTime::from_hms(19, 34, 00)).unwrap();

        let client = rocket::local::Client::new(server).unwrap();
        let mut response = client.get("/screenings").dispatch();
        assert_eq!(response.content_type(), Some(ContentType::JSON));

        let fetched_screenings: Vec<Screening> =
            serde_json::from_str(&response.body_string().unwrap()).unwrap();

        assert_eq!(fetched_screenings.len(), 1);

        let scr = &fetched_screenings[0];
        assert_eq!(scr.film.title, "Hellraiser");
        assert_eq!(scr.room.name, "Main room");
    });
}