forked from TWS/kalkutago
Compare commits
No commits in common. "4fb02e704c2e42047591f932fef47affcc4123a0" and "01004e237b644ab51ff6183a868a971a74e7d806" have entirely different histories.
4fb02e704c
...
01004e237b
3
Makefile
3
Makefile
|
@ -11,5 +11,4 @@ start-server: build-client
|
|||
|
||||
clean:
|
||||
docker compose down
|
||||
-rm -r server/public/ client/dist/
|
||||
|
||||
rm -r server/public/ client/dist/
|
||||
|
|
|
@ -26,7 +26,6 @@ class AppState {
|
|||
tracks: Array<Track>
|
||||
state: State
|
||||
user?: LoggedInUser
|
||||
source?: EventSource
|
||||
|
||||
constructor() {
|
||||
this.tracks = new Array<Track>
|
||||
|
@ -80,22 +79,16 @@ class AppState {
|
|||
window.location = window.location
|
||||
})
|
||||
window.addEventListener('beforeunload', () => source.close())
|
||||
this.source = source
|
||||
}
|
||||
async repopulate() {
|
||||
if (!this.user) {
|
||||
this.tracks = []
|
||||
return
|
||||
}
|
||||
this.state = State.Fetching
|
||||
this.tracks = await Track.fetchAll()
|
||||
this.source?.close()
|
||||
this.streamUpdatesFromServer()
|
||||
this.state = State.Fetched
|
||||
}
|
||||
async populate() {
|
||||
if (this.state != State.Unfetched) return
|
||||
await this.repopulate()
|
||||
this.streamUpdatesFromServer()
|
||||
this.state = State.Fetched
|
||||
}
|
||||
async taskCompleted(track: Track, date: Date): Promise<Tick> {
|
||||
const query = dateQuery(date)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import { error } from "./error"
|
||||
import { Tick, ITick } from './ticks'
|
||||
|
||||
export interface ITrack {
|
||||
id?: number
|
||||
|
@ -98,4 +97,4 @@ export class Track implements ITrack {
|
|||
}
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { state } from '../state';
|
||||
import router from '../router'
|
||||
|
||||
const name = ref("")
|
||||
const password = ref("")
|
||||
|
@ -10,13 +9,10 @@ async function signUp() {
|
|||
const $name = name.value
|
||||
const result = await fetch("/api/v1/auth", {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ name: $name, password: password.value }),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
body: JSON.stringify({ name: $name, password: password.value })
|
||||
})
|
||||
if (result.ok) {
|
||||
state.user = { name: $name }
|
||||
await state.repopulate()
|
||||
router.push("/")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,13 +20,10 @@ async function login() {
|
|||
const $name = name.value
|
||||
const result = await fetch("/api/v1/auth", {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({ name: $name, password: password.value }),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
body: JSON.stringify({ name: $name, password: password.value })
|
||||
})
|
||||
if (result.ok) {
|
||||
state.user = { name: $name }
|
||||
await state.repopulate()
|
||||
router.push("/")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,4 +66,4 @@ async function login() {
|
|||
.button.submit {
|
||||
margin-left: 10px;
|
||||
}
|
||||
</style>
|
||||
</style>
|
|
@ -1,10 +1,5 @@
|
|||
<script setup lang="ts">
|
||||
import Table from "../components/Table.vue";
|
||||
|
||||
import { state } from '../state.ts'
|
||||
import router from '../router.ts'
|
||||
|
||||
if(!state.user) router.push('/login')
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
version: "3.5"
|
||||
|
||||
services:
|
||||
server:
|
||||
build:
|
||||
context: ./server
|
||||
dockerfile: Dockerfile.test
|
||||
networks:
|
||||
- web
|
||||
- internal
|
||||
environment:
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres-password
|
||||
POSTGRES_USER: kalkutago
|
||||
POSTGRES_DB: kalkutago_TEST
|
||||
POSTGRES_HOST: database
|
||||
secrets: [ postgres-password, cookie-secret ]
|
||||
depends_on: [ database ]
|
||||
expose: [ 8000 ]
|
||||
volumes:
|
||||
- ./client/dist:/src/public:ro
|
||||
labels:
|
||||
traefik.enable: false
|
||||
|
||||
database:
|
||||
image: postgres
|
||||
environment:
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres-password
|
||||
POSTGRES_USER: kalkutago
|
||||
POSTGRES_DB: kalkutago_TEST
|
||||
secrets: [ postgres-password ]
|
||||
networks: [ internal ]
|
||||
labels:
|
||||
traefik.enable: false
|
||||
|
||||
secrets:
|
||||
postgres-password:
|
||||
file: ${PG_PW_FILE}
|
||||
cookie-secret:
|
||||
file: ${COOKIE_SECRET_FILE}
|
||||
|
||||
networks:
|
||||
internal:
|
||||
internal: true
|
||||
web:
|
||||
external: true
|
14
server/Cargo.lock
generated
14
server/Cargo.lock
generated
|
@ -1177,7 +1177,6 @@ dependencies = [
|
|||
"serde_json",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tokio-test",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2638,19 +2637,6 @@ dependencies = [
|
|||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-test"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53474327ae5e166530d17f2d956afcb4f8a004de581b3cae10f12006bc8163e3"
|
||||
dependencies = [
|
||||
"async-stream",
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-util"
|
||||
version = "0.7.8"
|
||||
|
|
|
@ -21,7 +21,6 @@ log = { version = "0.4.19", features = ["kv_unstable", "kv_unstable_serde"] }
|
|||
sea-orm-migration = "0.11.3"
|
||||
serde_json = "1.0.96"
|
||||
thiserror = "1.0.40"
|
||||
tokio-test = "0.4.2"
|
||||
|
||||
[dependencies.derive_builder]
|
||||
version = "0.12.0"
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
|
||||
|
||||
use derive_deref::Deref;
|
||||
use either::Either::{self, Right};
|
||||
|
||||
use log::{as_debug, as_serde, debug};
|
||||
use rocket::{
|
||||
http::{Cookie, CookieJar, Status},
|
||||
outcome::IntoOutcome,
|
||||
|
@ -10,7 +11,7 @@ use rocket::{
|
|||
Request, State,
|
||||
};
|
||||
use sea_orm::{prelude::*, DatabaseConnection};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
api::error::ApiResult,
|
||||
|
@ -20,10 +21,10 @@ use crate::{
|
|||
|
||||
use super::ErrorResponder;
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct LoginData {
|
||||
pub name: String,
|
||||
pub password: String,
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub(super) struct LoginData {
|
||||
name: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[put("/", data = "<user_data>", format = "application/json")]
|
||||
|
@ -60,7 +61,6 @@ pub(super) async fn sign_up(
|
|||
.insert(db as &DatabaseConnection)
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
debug!(user = as_serde!(user_data); "user added");
|
||||
cookies.add_private(Cookie::new(
|
||||
"user",
|
||||
serde_json::to_string(&user_data).map_err(Error::from)?,
|
||||
|
@ -73,23 +73,6 @@ pub(super) async fn sign_up(
|
|||
#[derive(Deref)]
|
||||
pub(super) struct Auth(users::Model);
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AuthData {
|
||||
id: i32,
|
||||
name: String,
|
||||
password_hash: String,
|
||||
}
|
||||
|
||||
impl From<AuthData> for Auth {
|
||||
fn from(value: AuthData) -> Self {
|
||||
Auth(users::Model {
|
||||
id: value.id,
|
||||
name: value.name,
|
||||
password_hash: value.password_hash,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for Auth {
|
||||
type Error = ();
|
||||
|
@ -98,13 +81,9 @@ impl<'r> FromRequest<'r> for Auth {
|
|||
let Some(user) = request.cookies().get_private("user") else {
|
||||
return request::Outcome::Failure(unauthorized);
|
||||
};
|
||||
let user = user.value();
|
||||
debug!(user = user; "user retreived from private cookie");
|
||||
let result = serde_json::from_str(user)
|
||||
serde_json::from_str(user.value())
|
||||
.ok()
|
||||
.map(|model: AuthData| model.into())
|
||||
.into_outcome(unauthorized);
|
||||
debug!(result = as_debug!(result); "auth FromRequest return value");
|
||||
result
|
||||
.map(Auth)
|
||||
.into_outcome(unauthorized)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ mod tracks;
|
|||
pub(crate) mod update;
|
||||
|
||||
use std::{
|
||||
default::Default,
|
||||
default::default,
|
||||
env, fs,
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
};
|
||||
|
@ -28,8 +28,6 @@ use tokio::sync::broadcast::{self, error::RecvError, Sender};
|
|||
use self::{error::ApiResult, update::Update};
|
||||
use log::{as_debug, as_serde, debug, trace};
|
||||
|
||||
pub use auth::LoginData;
|
||||
|
||||
#[get("/status")]
|
||||
fn status() -> &'static str {
|
||||
"Ok"
|
||||
|
@ -76,7 +74,7 @@ fn get_secret() -> [u8; 32] {
|
|||
data
|
||||
}
|
||||
|
||||
pub fn start_server(db: DatabaseConnection) -> Rocket<Build> {
|
||||
pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
|
||||
use groups::*;
|
||||
use ticks::*;
|
||||
use tracks::*;
|
||||
|
@ -85,7 +83,7 @@ pub fn start_server(db: DatabaseConnection) -> Rocket<Build> {
|
|||
.configure(Config {
|
||||
address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
||||
secret_key: SecretKey::derive_from(&get_secret()),
|
||||
..Config::default()
|
||||
..default()
|
||||
})
|
||||
.register("/", catchers![spa_index_redirect])
|
||||
.manage(db)
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
|
||||
|
||||
|
||||
use crate::api::auth::Auth;
|
||||
use crate::api::{self, error::ApiResult};
|
||||
use crate::entities::{prelude::*, *};
|
||||
use crate::error::Error;
|
||||
use either::Either::{self, Left, Right};
|
||||
use log::as_debug;
|
||||
use log::{as_serde, debug, warn};
|
||||
use rocket::http::Status;
|
||||
use rocket::{serde::json::Json, State};
|
||||
use sea_orm::{prelude::*, DatabaseConnection, IntoActiveModel, Statement};
|
||||
|
@ -77,11 +78,6 @@ pub(super) async fn insert_track(
|
|||
track: Json<serde_json::Value>,
|
||||
auth: Auth,
|
||||
) -> Result<Json<tracks::Model>, Either<Status, ErrorResponder>> {
|
||||
debug!(
|
||||
user=as_serde!(*auth),
|
||||
track=as_serde!(track.0);
|
||||
"authenticated user making track insertion request"
|
||||
);
|
||||
fn bad() -> Either<Status, ErrorResponder> {
|
||||
Left(Status::BadRequest)
|
||||
}
|
||||
|
@ -109,28 +105,17 @@ pub(super) async fn insert_track(
|
|||
user_id, track_id
|
||||
) select $1, ti.id
|
||||
from track_insertion ti
|
||||
join track_insertion using (id)
|
||||
returning id;"#,
|
||||
join track_insertion using (id);"#,
|
||||
[
|
||||
auth.id.into(),
|
||||
track
|
||||
.get("name")
|
||||
.ok_or_else(bad_value_for("name"))?
|
||||
.as_str()
|
||||
.ok_or_else(bad_value_for("name"))?
|
||||
.into(),
|
||||
track.get("name").ok_or_else(bad_value_for("name"))?.as_str().ok_or_else(bad_value_for("name"))?.into(),
|
||||
track
|
||||
.get("description")
|
||||
.ok_or_else(bad_value_for("description"))?
|
||||
.as_str()
|
||||
.ok_or_else(bad_value_for("description"))?
|
||||
.into(),
|
||||
track
|
||||
.get("icon")
|
||||
.ok_or_else(bad_value_for("icon"))?
|
||||
.as_str()
|
||||
.ok_or_else(bad_value_for("icon"))?
|
||||
.into(),
|
||||
track.get("icon").ok_or_else(bad_value_for("icon"))?.as_str().ok_or_else(bad_value_for("icon"))?.into(),
|
||||
track.get("enabled").and_then(|it| it.as_i64()).into(),
|
||||
track
|
||||
.get("multiple_entries_per_day")
|
||||
|
@ -141,21 +126,17 @@ pub(super) async fn insert_track(
|
|||
],
|
||||
))
|
||||
.await
|
||||
.map_err(|err| Right(Error::from(err).into()))?
|
||||
else {
|
||||
return Err(Right("no value returned from track insertion query".into()));
|
||||
};
|
||||
trace!("query completed");
|
||||
.map_err(|err| Right(Error::from(err).into()))? else {
|
||||
return Err(Right("no value returned from track insertion query".into()));
|
||||
};
|
||||
let track_id = track_id
|
||||
.try_get_by_index(0)
|
||||
.map_err(|err| Right(Error::from(err).into()))?;
|
||||
trace!(track_id = track_id; "freshly inserted track ID");
|
||||
let track = auth.authorized_track(track_id, db).await.ok_or_else(|| {
|
||||
Right(format!("failed to fetch freshly inserted track with id {track_id}").into())
|
||||
})?;
|
||||
if let Err(err) = tx.send(Update::track_added(track.clone())) {
|
||||
warn!(err = as_debug!(err); "error sending updates to subscribed channels");
|
||||
}
|
||||
tx.send(Update::track_added(track.clone()))
|
||||
.map_err(|err| Right(Error::from(err).into()))?;
|
||||
Ok(Json(track))
|
||||
}
|
||||
|
||||
|
@ -176,9 +157,8 @@ pub(super) async fn update_track(
|
|||
.update(db)
|
||||
.await
|
||||
.map_err(|err| Right(Error::from(err).into()))?;
|
||||
if let Err(err) = tx.send(Update::track_changed(track.clone())) {
|
||||
warn!(err = as_debug!(err); "error sending updates to subscribed channels");
|
||||
}
|
||||
tx.send(Update::track_changed(track.clone()))
|
||||
.map_err(|err| Right(Error::from(err).into()))?;
|
||||
Ok(Json(track))
|
||||
}
|
||||
|
||||
|
@ -213,10 +193,10 @@ pub(super) async fn ticked(
|
|||
let tick = tick
|
||||
.insert(db as &DatabaseConnection)
|
||||
.await
|
||||
.map_err(|err| Right(Error::from(err).into()))?
|
||||
;
|
||||
tx.send(Update::tick_added(tick.clone()))
|
||||
.map_err(|err| Right(Error::from(err).into()))?;
|
||||
if let Err(err) = tx.send(Update::tick_added(tick.clone())) {
|
||||
warn!(err = as_debug!(err); "error sending updates to subscribed channels");
|
||||
}
|
||||
Ok(Json(tick))
|
||||
}
|
||||
|
||||
|
@ -241,10 +221,10 @@ pub(super) async fn ticked_on_date(
|
|||
let tick = tick
|
||||
.insert(db as &DatabaseConnection)
|
||||
.await
|
||||
.map_err(Error::from)?
|
||||
;
|
||||
tx.send(Update::tick_added(tick.clone()))
|
||||
.map_err(Error::from)?;
|
||||
if let Err(err) = tx.send(Update::tick_added(tick.clone())) {
|
||||
warn!(err = as_debug!(err); "error sending updates to subscribed channels");
|
||||
}
|
||||
Ok(Left(Json(tick)))
|
||||
}
|
||||
|
||||
|
@ -270,9 +250,7 @@ pub(super) async fn clear_all_ticks(
|
|||
.map_err(Error::from)?;
|
||||
for tick in ticks.clone() {
|
||||
tick.clone().delete(db).await.map_err(Error::from)?;
|
||||
if let Err(err) = Update::tick_cancelled(tick).send(tx) {
|
||||
warn!(err = as_debug!(err); "error sending updates to subscribed channels");
|
||||
}
|
||||
Update::tick_cancelled(tick).send(tx)?;
|
||||
}
|
||||
Ok(Right(Json(ticks)))
|
||||
}
|
||||
|
@ -301,9 +279,7 @@ pub(super) async fn clear_all_ticks_on_day(
|
|||
.map_err(Error::from)?;
|
||||
for tick in ticks.clone() {
|
||||
tick.clone().delete(db).await.map_err(Error::from)?;
|
||||
if let Err(err) = Update::tick_cancelled(tick).send(tx) {
|
||||
warn!(err = as_debug!(err); "error sending updates to subscribed channels");
|
||||
}
|
||||
Update::tick_cancelled(tick).send(tx)?;
|
||||
}
|
||||
Ok(Right(Json(ticks)))
|
||||
}
|
||||
|
|
|
@ -91,13 +91,8 @@ impl Update {
|
|||
}
|
||||
|
||||
pub fn send(self, tx: &Sender<Self>) -> Result<()> {
|
||||
let receiver_count = tx.receiver_count();
|
||||
if receiver_count > 0 {
|
||||
trace!(receiver_count = receiver_count, update = as_serde!(self); "sending update");
|
||||
let count = tx.send(self.clone())?;
|
||||
} else {
|
||||
trace!("no update receivers, skipping message");
|
||||
}
|
||||
let count = tx.send(self.clone())?;
|
||||
trace!(sent_to = count, update = as_serde!(self); "sent update to SSE channel");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,11 @@
|
|||
use crate::migrator::Migrator;
|
||||
use sea_orm_migration::MigratorTrait;
|
||||
use sea_orm_migration::SchemaManager;
|
||||
use std::{
|
||||
default::default,
|
||||
env,
|
||||
ffi::{OsStr, OsString},
|
||||
fs::File,
|
||||
io::Read,
|
||||
};
|
||||
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
|
||||
// from https://doc.rust-lang.org/std/ffi/struct.OsString.html
|
||||
fn concat_os_strings(a: &OsStr, b: &OsStr) -> OsString {
|
||||
let mut ret = OsString::with_capacity(a.len() + b.len()); // This will allocate
|
||||
|
@ -34,7 +30,7 @@ fn get_env_var_or_file<A: AsRef<OsStr>>(key: A) -> Option<String> {
|
|||
if let Some(path) = env::var_os(file_key) {
|
||||
// open the file and read it
|
||||
let mut file = File::open(&path).unwrap_or_else(|_| panic!("no such file at {path:?}"));
|
||||
let mut val = String::new();
|
||||
let mut val: String = default();
|
||||
file.read_to_string(&mut val)
|
||||
.unwrap_or_else(|_| panic!("reading file at {path:?}"));
|
||||
Some(val)
|
||||
|
@ -62,31 +58,3 @@ pub fn connection_url() -> String {
|
|||
.unwrap_or(5432_u16);
|
||||
format!("postgres://{user}:{password}@{host}:{port}/{db}")
|
||||
}
|
||||
|
||||
pub async fn connection() -> DatabaseConnection {
|
||||
Database::connect(connection_url())
|
||||
.await
|
||||
.expect("db connection")
|
||||
}
|
||||
pub async fn migrated() -> DatabaseConnection {
|
||||
let db = connection().await;
|
||||
let schema_manager = SchemaManager::new(&db);
|
||||
Migrator::refresh(&db).await.expect("migration");
|
||||
assert!(schema_manager
|
||||
.has_table("tracks")
|
||||
.await
|
||||
.expect("fetch tracks table"));
|
||||
assert!(schema_manager
|
||||
.has_table("ticks")
|
||||
.await
|
||||
.expect("fetch ticks table"));
|
||||
assert!(schema_manager
|
||||
.has_table("groups")
|
||||
.await
|
||||
.expect("fetch groups table"));
|
||||
assert!(schema_manager
|
||||
.has_table("track2_groups")
|
||||
.await
|
||||
.expect("fetch track2groups table"));
|
||||
db
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use std::default::Default;
|
||||
use std::default::default;
|
||||
|
||||
use chrono::{Datelike, Timelike, Utc};
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
@ -60,7 +60,7 @@ impl ActiveModel {
|
|||
minute: Set(now.minute().try_into().ok()),
|
||||
second: Set(now.second().try_into().ok()),
|
||||
has_time_info: Set(Some(1)),
|
||||
..Default::default()
|
||||
..default()
|
||||
}
|
||||
}
|
||||
pub(crate) fn on(date: Date, track_id: i32) -> Self {
|
||||
|
@ -80,7 +80,7 @@ impl ActiveModel {
|
|||
minute: Set(now.minute().try_into().ok()),
|
||||
second: Set(now.second().try_into().ok()),
|
||||
has_time_info: Set(Some(1)),
|
||||
..Default::default()
|
||||
..default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use std::default::Default;
|
||||
use std::default::default;
|
||||
|
||||
use bcrypt::*;
|
||||
// TODO Add option for argon2 https://docs.rs/argon2/latest/argon2/
|
||||
|
@ -57,7 +57,7 @@ impl ActiveModel {
|
|||
Ok(Self {
|
||||
name,
|
||||
password_hash,
|
||||
..Default::default()
|
||||
..default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
#![feature(proc_macro_hygiene, decl_macro, never_type)]
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
pub mod api;
|
||||
pub mod db;
|
||||
pub mod entities;
|
||||
pub mod error;
|
||||
mod migrator;
|
|
@ -1,4 +1,4 @@
|
|||
#![feature(proc_macro_hygiene, decl_macro, never_type)]
|
||||
#![feature(default_free_fn, proc_macro_hygiene, decl_macro, never_type)]
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
mod api;
|
||||
|
@ -6,9 +6,32 @@ mod db;
|
|||
mod entities;
|
||||
mod error;
|
||||
mod migrator;
|
||||
use crate::migrator::Migrator;
|
||||
use sea_orm::Database;
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[launch]
|
||||
async fn rocket_defines_the_main_fn() -> _ {
|
||||
femme::with_level(femme::LevelFilter::Trace);
|
||||
api::start_server(db::migrated().await)
|
||||
femme::with_level(femme::LevelFilter::Debug);
|
||||
let url = db::connection_url();
|
||||
let db = Database::connect(url).await.expect("db connection");
|
||||
let schema_manager = SchemaManager::new(&db);
|
||||
Migrator::refresh(&db).await.expect("migration");
|
||||
assert!(schema_manager
|
||||
.has_table("tracks")
|
||||
.await
|
||||
.expect("fetch tracks table"));
|
||||
assert!(schema_manager
|
||||
.has_table("ticks")
|
||||
.await
|
||||
.expect("fetch ticks table"));
|
||||
assert!(schema_manager
|
||||
.has_table("groups")
|
||||
.await
|
||||
.expect("fetch groups table"));
|
||||
assert!(schema_manager
|
||||
.has_table("track2_groups")
|
||||
.await
|
||||
.expect("fetch track2groups table"));
|
||||
api::start_server(db)
|
||||
}
|
||||
|
|
14
shell.nix
14
shell.nix
|
@ -1,14 +0,0 @@
|
|||
# DEVELOPMENT shell environment
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
|
||||
pkgs.mkShell {
|
||||
nativeBuildInputs = with pkgs.buildPackages; [
|
||||
clang
|
||||
yarn nodejs
|
||||
openssl
|
||||
python3
|
||||
python3Packages.requests
|
||||
python3Packages.ipython
|
||||
];
|
||||
}
|
||||
|
61
test.py
61
test.py
|
@ -1,61 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
#
|
||||
# Quick script to test endpoints of kalkutago
|
||||
|
||||
from requests import get, post, put, patch
|
||||
from time import gmtime as utc
|
||||
|
||||
credentials = {"name": "testuser", "password": "testpass"}
|
||||
track = {"name": "test", "description": "test track", "icon": "❓", "enabled": 1}
|
||||
|
||||
def test_auth(method):
|
||||
res = method(f'http://kalkutago/api/v1/auth', json=credentials)
|
||||
assert 'user' in res.cookies.iterkeys(), \
|
||||
f'no user cookie found. Cookies: {res.cookies.get_dict()}; body: ' + \
|
||||
res.text
|
||||
return res.cookies['user']
|
||||
|
||||
def test_create_user():
|
||||
return test_auth(post)
|
||||
|
||||
def test_login():
|
||||
return test_auth(put)
|
||||
|
||||
def test_track_creation(auth_cookie):
|
||||
res = post('http://kalkutago/api/v1/tracks', json=track,
|
||||
cookies={'user': auth_cookie})
|
||||
print(res.text)
|
||||
res.raise_for_status()
|
||||
return res.json()
|
||||
|
||||
def test_get_track(auth_cookie, track):
|
||||
res = get(f'http://kalkutago/api/v1/tracks/{track["id"]}',
|
||||
cookies={'user': auth_cookie})
|
||||
print(res.text)
|
||||
res.raise_for_status()
|
||||
retrieved = res.json()
|
||||
assert track == retrieved, f'expected {track!r} to equal {retrieved!r}'
|
||||
return retrieved
|
||||
|
||||
def test_tick(auth_cookie, track):
|
||||
res = patch(f'http://kalkutago/api/v1/tracks/{track["id"]}/ticked',
|
||||
cookies={'user': auth_cookie})
|
||||
print(res.text)
|
||||
res.raise_for_status()
|
||||
retrieved = res.json()
|
||||
# result:
|
||||
# {"id":1,"track_id":6,"year":2023,"month":8,"day":10,"hour":13,"minute":7,"second":41,"has_time_info":1}
|
||||
now = utc()
|
||||
assert retrieved['track_id'] == track['id']
|
||||
assert retrieved['year'] == now.tm_year
|
||||
assert retrieved['month'] == now.tm_mon
|
||||
assert retrieved['day'] == now.tm_mday
|
||||
return retrieved
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
login_cookie = test_create_user()
|
||||
test_login()
|
||||
track = test_track_creation(login_cookie)
|
||||
retrieved = test_get_track(login_cookie, track)
|
||||
tick = test_tick(login_cookie, track)
|
Loading…
Reference in a new issue