Test permissions use api (#784)

* initial push

* fmt; clippy

---------

Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
This commit is contained in:
Wyatt Verchere
2023-12-11 18:24:49 -08:00
committed by GitHub
parent 27ccd3dfa8
commit 6217523cc8
32 changed files with 1558 additions and 1015 deletions

View File

@@ -5,6 +5,8 @@ use common::{database::*, environment::with_test_environment_all};
use labrinth::models::pats::Scopes;
use serde_json::json;
use crate::common::api_common::AppendsOptionalPat;
mod common;
// Full pat test:
@@ -20,7 +22,7 @@ pub async fn pat_full_test() {
// Create a PAT for a full test
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
"name": "test_pat_scopes Test",
@@ -42,7 +44,7 @@ pub async fn pat_full_test() {
// Get PAT again
let req = test::TestRequest::get()
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.uri("/_internal/pat")
.to_request();
let resp = test_env.call(req).await;
@@ -76,7 +78,7 @@ pub async fn pat_full_test() {
// Change scopes and test again
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": 0,
}))
@@ -88,7 +90,7 @@ pub async fn pat_full_test() {
// Change scopes back, and set expiry to the past, and test again
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE,
"expires": Utc::now() + Duration::seconds(1), // expires in 1 second
@@ -104,7 +106,7 @@ pub async fn pat_full_test() {
// Change everything back to normal and test again
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"expires": Utc::now() + Duration::days(1), // no longer expired!
}))
@@ -116,7 +118,7 @@ pub async fn pat_full_test() {
// Patching to a bad expiry should fail
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"expires": Utc::now() - Duration::days(1), // Past
}))
@@ -133,7 +135,7 @@ pub async fn pat_full_test() {
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": scope.bits(),
}))
@@ -147,7 +149,7 @@ pub async fn pat_full_test() {
// Delete PAT
let req = test::TestRequest::delete()
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.uri(&format!("/_internal/pat/{}", id))
.to_request();
let resp = test_env.call(req).await;
@@ -163,7 +165,7 @@ pub async fn bad_pats() {
// Creating a PAT with no name should fail
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
"expires": Utc::now() + Duration::days(1),
@@ -176,7 +178,7 @@ pub async fn bad_pats() {
for name in ["n", "this_name_is_too_long".repeat(16).as_str()] {
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"name": name,
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
@@ -190,7 +192,7 @@ pub async fn bad_pats() {
// Creating a PAT with an expiry in the past should fail
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE, // Collection create as an easily tested example
"name": "test_pat_scopes Test",
@@ -208,7 +210,7 @@ pub async fn bad_pats() {
}
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": scope.bits(),
"name": format!("test_pat_scopes Name {}", i),
@@ -225,7 +227,7 @@ pub async fn bad_pats() {
// Create a 'good' PAT for patching
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": Scopes::COLLECTION_CREATE,
"name": "test_pat_scopes Test",
@@ -241,7 +243,7 @@ pub async fn bad_pats() {
for name in ["n", "this_name_is_too_long".repeat(16).as_str()] {
let req = test::TestRequest::post()
.uri("/_internal/pat")
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"name": name,
}))
@@ -253,7 +255,7 @@ pub async fn bad_pats() {
// Patching to a bad expiry should fail
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"expires": Utc::now() - Duration::days(1), // Past
}))
@@ -270,7 +272,7 @@ pub async fn bad_pats() {
let req = test::TestRequest::patch()
.uri(&format!("/_internal/pat/{}", id))
.append_header(("Authorization", USER_USER_PAT))
.append_pat(USER_USER_PAT)
.set_json(json!({
"scopes": scope.bits(),
}))