Skip to content

Commit a79163d

Browse files
committed
tests: Fix tests.
1 parent 3336a94 commit a79163d

3 files changed

Lines changed: 69 additions & 40 deletions

File tree

tests/mailinglist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use serde_json::Value;
77
use zauth::DbConn;
88
use zauth::models::client::{Client, NewClient};
99
use zauth::models::mail::NewMail;
10-
use zauth::models::role::NewRole;
1110
use zauth::models::role::Role;
11+
use zauth::models::role::{NewRole, RoleVisibility};
1212
use zauth::models::user::*;
1313

1414
use crate::common::{HttpClient, config};
@@ -335,7 +335,7 @@ async fn authorized_client_can_use_mailinglist() {
335335
NewRole {
336336
name: config().mailer_role.to_string(),
337337
description: "test".into(),
338-
client_id: None,
338+
visibility: RoleVisibility::Global,
339339
},
340340
&db,
341341
)

tests/oauth.rs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use zauth::controllers::oauth_controller::UserToken;
1717
use zauth::models::client::{Client, NewClient};
1818
use zauth::models::role::NewRole;
1919
use zauth::models::role::Role;
20+
use zauth::models::role::RoleVisibility;
2021
use zauth::models::user::{NewUser, User};
2122
use zauth::token_store::TokenStore;
2223

@@ -63,17 +64,30 @@ async fn create_client(db: &DbConn, name: &str) -> Client {
6364
client.update(db).await.expect("client updated")
6465
}
6566

66-
async fn create_role(db: &DbConn, name: &str, client_id: Option<i32>) -> Role {
67-
Role::create(
67+
async fn create_role(
68+
db: &DbConn,
69+
name: &str,
70+
visibility: RoleVisibility,
71+
limited_to_client_ids: Vec<i32>,
72+
) -> Role {
73+
let role = Role::create(
6874
NewRole {
6975
name: name.into(),
7076
description: "test".into(),
71-
client_id,
77+
visibility,
7278
},
7379
db,
7480
)
7581
.await
76-
.expect("role created")
82+
.expect("role created");
83+
84+
for client_id in limited_to_client_ids {
85+
role.add_client_to_limited_to(client_id, db)
86+
.await
87+
.expect("limited_to client added to role");
88+
}
89+
90+
role
7791
}
7892

7993
// Test all the usual oauth requests until `access_token/id_token` is retrieved.
@@ -363,14 +377,37 @@ async fn roles_flow() {
363377
let user = create_user(&db).await;
364378
let client = create_client(&db, CLIENT_ID).await;
365379
let client_not_used = create_client(&db, "not_used").await;
366-
let role_global = create_role(&db, "global", None).await;
367-
let role_client = create_role(&db, "client", Some(client.id)).await;
368-
let role_client_not_used =
369-
create_role(&db, "client_not_used", Some(client_not_used.id)).await;
370-
let _role_global_not_mapped =
371-
create_role(&db, "global_not_mapped", None).await;
372-
let _role_client_not_mapped =
373-
create_role(&db, "client_not_mapped", Some(client.id)).await;
380+
let role_global =
381+
create_role(&db, "global", RoleVisibility::Global, Vec::from([]))
382+
.await;
383+
let role_client = create_role(
384+
&db,
385+
"client",
386+
RoleVisibility::Limited,
387+
Vec::from([client.id]),
388+
)
389+
.await;
390+
let role_client_not_used = create_role(
391+
&db,
392+
"client_not_used",
393+
RoleVisibility::Limited,
394+
Vec::from([client_not_used.id]),
395+
)
396+
.await;
397+
let _role_global_not_mapped = create_role(
398+
&db,
399+
"global_not_mapped",
400+
RoleVisibility::Global,
401+
Vec::from([]),
402+
)
403+
.await;
404+
let _role_client_not_mapped = create_role(
405+
&db,
406+
"client_not_mapped",
407+
RoleVisibility::Limited,
408+
Vec::from([client.id]),
409+
)
410+
.await;
374411

375412
role_global
376413
.add_user(user.id, &db)
@@ -456,7 +493,9 @@ async fn roles_flow() {
456493
async fn client_credentials_flow() {
457494
common::as_visitor(async move |http_client, db| {
458495
let client = create_client(&db, CLIENT_ID).await;
459-
let role_global = create_role(&db, "global", None).await;
496+
let role_global =
497+
create_role(&db, "global", RoleVisibility::Global, Vec::from([]))
498+
.await;
460499
role_global
461500
.add_client(client.id, &db)
462501
.await

tests/roles.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ mod common;
22

33
use rocket::http::{Accept, ContentType, Status};
44

5-
use zauth::models::client::{Client, NewClient};
6-
use zauth::models::role::{NewRole, Role};
5+
use zauth::models::role::{NewRole, Role, RoleVisibility};
76
use zauth::models::user::User;
87

98
use crate::common::HttpClient;
@@ -51,8 +50,9 @@ async fn create_role_as_user() {
5150
async fn create_global_role() {
5251
common::as_admin(async move |http_client: HttpClient, db, _user| {
5352
let role_name = "test";
54-
let role_form =
55-
format!("name={role_name}&description=test_description");
53+
let role_form = format!(
54+
"name={role_name}&description=test_description&visibility=global"
55+
);
5656

5757
let response = http_client
5858
.post("/roles")
@@ -70,27 +70,17 @@ async fn create_global_role() {
7070

7171
assert_eq!(created.name, role_name);
7272
assert_eq!(created.description, "test_description");
73-
assert_eq!(created.client_id, None);
73+
assert_eq!(created.visibility, RoleVisibility::Global);
7474
})
7575
.await;
7676
}
7777

7878
#[rocket::async_test]
79-
async fn create_client_role() {
79+
async fn create_limited_role() {
8080
common::as_admin(async move |http_client: HttpClient, db, _user| {
81-
let client = Client::create(
82-
NewClient {
83-
name: String::from("test"),
84-
},
85-
&db,
86-
)
87-
.await
88-
.unwrap();
89-
9081
let role_name = "test";
9182
let role_form = format!(
92-
"name={role_name}&description=test_description&client_id={}",
93-
client.id
83+
"name={role_name}&description=test_description&visibility=limited",
9484
);
9585

9686
let response = http_client
@@ -109,7 +99,7 @@ async fn create_client_role() {
10999

110100
assert_eq!(created.name, role_name);
111101
assert_eq!(created.description, "test_description");
112-
assert_eq!(created.client_id, Some(client.id));
102+
assert_eq!(created.visibility, RoleVisibility::Limited);
113103
})
114104
.await;
115105
}
@@ -121,7 +111,7 @@ async fn show_role_as_user() {
121111
NewRole {
122112
name: "test".into(),
123113
description: "test".into(),
124-
client_id: None,
114+
visibility: RoleVisibility::Global,
125115
},
126116
&db,
127117
)
@@ -145,7 +135,7 @@ async fn show_role_as_admin() {
145135
NewRole {
146136
name: "test".into(),
147137
description: "test".into(),
148-
client_id: None,
138+
visibility: RoleVisibility::Global,
149139
},
150140
&db,
151141
)
@@ -168,7 +158,7 @@ async fn delete_role() {
168158
NewRole {
169159
name: "test".into(),
170160
description: "test".into(),
171-
client_id: None,
161+
visibility: RoleVisibility::Global,
172162
},
173163
&db,
174164
)
@@ -194,7 +184,7 @@ async fn add_user_to_role_as_user() {
194184
NewRole {
195185
name: "test".into(),
196186
description: "test".into(),
197-
client_id: None,
187+
visibility: RoleVisibility::Global,
198188
},
199189
&db,
200190
)
@@ -223,7 +213,7 @@ async fn add_user_to_role_as_admin() {
223213
NewRole {
224214
name: "test".into(),
225215
description: "test".into(),
226-
client_id: None,
216+
visibility: RoleVisibility::Global,
227217
},
228218
&db,
229219
)
@@ -264,7 +254,7 @@ async fn add_role_to_user_as_user() {
264254
NewRole {
265255
name: "test".into(),
266256
description: "test".into(),
267-
client_id: None,
257+
visibility: RoleVisibility::Global,
268258
},
269259
&db,
270260
)
@@ -293,7 +283,7 @@ async fn add_role_to_user_as_admin() {
293283
NewRole {
294284
name: "test".into(),
295285
description: "test".into(),
296-
client_id: None,
286+
visibility: RoleVisibility::Global,
297287
},
298288
&db,
299289
)

0 commit comments

Comments
 (0)