Skip to content

Commit

Permalink
refactor(profile): allow anonymous to get user profile (#12)
Browse files Browse the repository at this point in the history
* refactor(profile): allow anonymous to get user profile

* fix(test): fix profile to get
  • Loading branch information
fu050409 authored Nov 28, 2024
1 parent e37b626 commit e28b7a2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
7 changes: 7 additions & 0 deletions .changes/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"gitSiteUrl": "https://github.com/swpu-acm/online-judge/",
"changeTags": {
"feat": "New Features",
"fix": "Bug Fixes",
"perf": "Performance Improvements",
"chore": "Chores",
"refactor": "Refactors"
},
"pkgManagers": {
"rust": {
"version": true,
Expand Down
5 changes: 5 additions & 0 deletions .changes/refactor-profile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"algohub-server": patch:refactor
---

Refactor api for getting user profile, allow get user profile anonymously
6 changes: 6 additions & 0 deletions src/models/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ pub struct OwnedCredentials {
pub id: String,
pub token: String,
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Token<'r> {
pub token: &'r str,
}
28 changes: 4 additions & 24 deletions src/routes/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
account::Profile,
error::{Error, ErrorResponse},
response::{Empty, Response},
Record,
Record, Token,
},
utils::{account, session},
Result,
Expand Down Expand Up @@ -104,24 +104,8 @@ pub async fn profile(db: &State<Surreal<Client>>, profile: Json<ProfileData<'_>>
}
}

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Authenticate<'r> {
pub token: &'r str,
}

#[post("/profile/<id>", data = "<auth>")]
pub async fn get_profile(
db: &State<Surreal<Client>>,
id: &str,
auth: Json<Authenticate<'_>>,
) -> Result<Profile> {
if !session::verify(db, id, auth.token).await {
return Err(Error::Unauthorized(Json(
"Failed to grant access permission".into(),
)));
}

#[get("/profile/<id>")]
pub async fn get_profile(db: &State<Surreal<Client>>, id: &str) -> Result<Profile> {
let profile = account::get_by_id::<Profile>(db, id)
.await
.map_err(|e| Error::NotFound(Json(e.to_string().into())))?
Expand Down Expand Up @@ -209,11 +193,7 @@ pub async fn upload_content(
}

#[post("/delete/<id>", data = "<auth>")]
pub async fn delete(
db: &State<Surreal<Client>>,
id: &str,
auth: Json<Authenticate<'_>>,
) -> Result<Empty> {
pub async fn delete(db: &State<Surreal<Client>>, id: &str, auth: Json<Token<'_>>) -> Result<Empty> {
if !session::verify(db, id, auth.token).await {
return Err(Error::Unauthorized(Json(
"Failed to grant access permission".into(),
Expand Down
10 changes: 5 additions & 5 deletions tests/account.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{fs::File, io::Read, path::Path};

use anyhow::Result;
use algohub_server::{
models::{
account::Profile,
response::{Empty, Response},
Token,
},
routes::account::{Authenticate, ProfileData, RegisterData, RegisterResponse, UploadResponse},
routes::account::{ProfileData, RegisterData, RegisterResponse, UploadResponse},
};
use anyhow::Result;
use rocket::{http::ContentType, local::asynchronous::Client};

pub struct Upload {
Expand Down Expand Up @@ -127,8 +128,7 @@ async fn test_register() -> Result<()> {
assert!(empty_data.is_none());

let response = client
.post(format!("/account/profile/{}", &id))
.json(&Authenticate { token: &token })
.get(format!("/account/profile/{}", &id))
.dispatch()
.await;

Expand Down Expand Up @@ -169,7 +169,7 @@ async fn test_register() -> Result<()> {

let response = client
.post(format!("/account/delete/{}", id))
.json(&Authenticate { token: &token })
.json(&Token { token: &token })
.dispatch()
.await;

Expand Down
6 changes: 3 additions & 3 deletions tests/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use algohub_server::{
models::{
problem::{Mode, ProblemDetail},
response::{Empty, Response},
OwnedCredentials,
OwnedCredentials, Token,
},
routes::{
account::{Authenticate, RegisterData, RegisterResponse},
account::{RegisterData, RegisterResponse},
problem::{ListProblem, ProblemData, ProblemFilter, ProblemResponse},
},
};
Expand Down Expand Up @@ -131,7 +131,7 @@ async fn test_problem() -> Result<()> {

client
.post(format!("/account/delete/{}", id))
.json(&Authenticate { token: &token })
.json(&Token { token: &token })
.dispatch()
.await
.into_json::<Response<Empty>>()
Expand Down

0 comments on commit e28b7a2

Please sign in to comment.