1use std::fmt;
2use std::marker::PhantomData;
3
4use sofie_core::Uuid;
5use sofie_db::Id;
6use sofie_http::{Area, PrincipalRecord, RoutePath};
7
8use crate::TestApp;
9use crate::fixture::fixture_value;
10use crate::messages::now;
11use crate::request::{TestRequest, TestSession};
12
13pub struct TestPrincipal<A: Area> {
14 id: Uuid,
15 area: PhantomData<fn() -> A>,
16}
17
18impl<A: Area> TestPrincipal<A> {
19 #[must_use]
20 pub fn new(id: Id<PrincipalRecord<A>>) -> Self {
21 Self {
22 id: id.uuid(),
23 area: PhantomData,
24 }
25 }
26
27 #[must_use]
28 pub fn fresh(app: &TestApp) -> Self {
29 Self::new(Id::from_uuid(fixture_value(app)))
30 }
31
32 #[must_use]
33 pub fn id(&self) -> Id<PrincipalRecord<A>> {
34 Id::from_uuid(self.id)
35 }
36}
37
38impl<A: Area> fmt::Debug for TestPrincipal<A> {
39 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
40 write!(formatter, "TestPrincipal<{}>({})", A::NAME, self.id)
41 }
42}
43
44pub struct TestCaller<'app> {
45 app: &'app TestApp,
46 session: TestSession,
47}
48
49impl<'app> TestCaller<'app> {
50 pub(crate) fn new<A: Area>(app: &'app TestApp, principal: &TestPrincipal<A>) -> Self {
51 Self {
52 app,
53 session: app.server.seal(A::NAME, principal.id, now(app)),
54 }
55 }
56
57 pub fn get(&self, path: &str) -> TestRequest {
58 self.app.get(path).session(&self.session)
59 }
60
61 pub fn post(&self, path: &str) -> TestRequest {
62 self.app.post(path).session(&self.session)
63 }
64
65 pub fn request(&self, route: &RoutePath) -> TestRequest {
66 self.app.request(route).session(&self.session)
67 }
68}