Remove API tests
This commit is contained in:
parent
42d212dc95
commit
696cadb5f5
1530
Cargo.lock
generated
1530
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
19
Cargo.toml
|
|
@ -1,19 +0,0 @@
|
|||
[package]
|
||||
name = "test"
|
||||
version = "0.1.0"
|
||||
authors = ["Pig Fang <g-plane@hotmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
path = "tests/Api/tests/main.rs"
|
||||
doctest = false
|
||||
|
||||
[dev-dependencies]
|
||||
reqwest = "0.9"
|
||||
rusqlite = "0.18"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[target.'cfg(unix)'.dev-dependencies]
|
||||
openssl = "0.10"
|
||||
openssl-sys = "0.9"
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
export APP_ENV="testing"
|
||||
export DB_CONNECTION="sqlite"
|
||||
export DB_DATABASE="/tmp/test.db"
|
||||
|
||||
if [ -e $DB_DATABASE ]; then rm $DB_DATABASE; fi;
|
||||
touch $DB_DATABASE
|
||||
if [ ! -e .env ]; then cp .env.testing .env ; fi;
|
||||
|
||||
php artisan bs:install ibara.mayaka@api.test 12345678 hyouka --quiet
|
||||
php artisan serve --port 32123 --quiet &
|
||||
cargo test -- --test-threads=1
|
||||
|
||||
kill $(ps -ef | grep 'php artisan serve' | awk '{print $2}' | awk -F"/" '{print $1}' | head -n 1)
|
||||
kill $(ps -ef | grep 'php -S' | awk '{print $2}' | awk -F"/" '{print $1}' | head -n 1)
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
## What's here?
|
||||
|
||||
Code in this directory is used for Blessing Skin API integration tests. For most developers, you don't need to concern about these files.
|
||||
|
||||
We use Rust to write tests to enforce us to write type-friendly APIs.
|
||||
|
||||
If you want to contribute to these, you're required to know about Rust and Cargo.
|
||||
|
|
@ -1,66 +0,0 @@
|
|||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Token {
|
||||
token: String,
|
||||
}
|
||||
|
||||
pub fn login() -> String {
|
||||
let client = reqwest::Client::new();
|
||||
let mut response = client
|
||||
.post("http://127.0.0.1:32123/api/auth/login")
|
||||
.json(&json!({
|
||||
"email": "ibara.mayaka@api.test",
|
||||
"password": "12345678"
|
||||
}))
|
||||
.send()
|
||||
.unwrap();
|
||||
let Token { token } = response.json().unwrap();
|
||||
format!("Bearer {}", token)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn successful_login() {
|
||||
assert_ne!(login(), "Bearer ".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_login() {
|
||||
let client = reqwest::Client::new();
|
||||
let mut response = client
|
||||
.post("http://127.0.0.1:32123/api/auth/login")
|
||||
.json(&json!({
|
||||
"email": "ibara.mayaka@api.test",
|
||||
"password": "wrong-password"
|
||||
}))
|
||||
.send()
|
||||
.unwrap();
|
||||
let Token { token } = response.json().unwrap();
|
||||
assert_eq!(token, "".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logout() {
|
||||
let token = login();
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.post("http://127.0.0.1:32123/api/auth/logout")
|
||||
.header("Authorization", token)
|
||||
.send()
|
||||
.unwrap();
|
||||
assert_eq!(response.status(), reqwest::StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refresh_token() {
|
||||
let token = login();
|
||||
let client = reqwest::Client::new();
|
||||
let mut response = client
|
||||
.post("http://127.0.0.1:32123/api/auth/refresh")
|
||||
.header("Authorization", token.clone())
|
||||
.send()
|
||||
.unwrap();
|
||||
let Token { token: new_token } = response.json().unwrap();
|
||||
assert_ne!(token, new_token);
|
||||
}
|
||||
|
|
@ -1,162 +0,0 @@
|
|||
use crate::auth::login;
|
||||
use crate::types::JsonBody;
|
||||
use rusqlite::{params, Connection};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
use std::env;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Closet {
|
||||
pub category: String,
|
||||
pub total_pages: usize,
|
||||
pub items: Vec<ClosetItem>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ClosetItem {
|
||||
pub tid: u32,
|
||||
pub name: String,
|
||||
pub r#type: String,
|
||||
pub size: u32,
|
||||
pub hash: String,
|
||||
pub uploader: u32,
|
||||
pub public: bool,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fetch_closet_info() {
|
||||
let token = login();
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/closet")
|
||||
.header("Authorization", token.clone())
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Closet>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let closet = body.data().unwrap();
|
||||
assert_eq!(closet.category, "skin");
|
||||
assert_eq!(closet.total_pages, 0);
|
||||
assert_eq!(closet.items.len(), 0);
|
||||
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/closet")
|
||||
.header("Authorization", token)
|
||||
.json(&json!({"category": "cape"}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Closet>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let closet = body.data().unwrap();
|
||||
assert_eq!(closet.category, "cape");
|
||||
assert_eq!(closet.total_pages, 0);
|
||||
assert_eq!(closet.items.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn insert_to_closet() {
|
||||
let conn = Connection::open(env::var("DB_DATABASE").unwrap()).unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO textures (name, type, hash, size, uploader, public, upload_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||
params!["steve", "steve", "abc", 1, 1, 1, "2019-01-01 00:00:00"],
|
||||
)
|
||||
.unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO textures (name, type, hash, size, uploader, public, upload_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||
params!["cape", "cape", "def", 1, 1, 1, "2019-01-01 00:00:00"],
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let token = login();
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let body = client
|
||||
.post("http://127.0.0.1:32123/api/closet")
|
||||
.header("Authorization", token.clone())
|
||||
.json(&json!({"tid": 1, "name": "my-first-texture"}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<()>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/closet")
|
||||
.header("Authorization", token)
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Closet>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let closet = body.data().unwrap();
|
||||
assert_eq!(closet.total_pages, 1);
|
||||
assert_eq!(closet.items.len(), 1);
|
||||
let item = closet.items.get(0).unwrap();
|
||||
assert_eq!(item.tid, 1);
|
||||
assert_eq!(item.name, "my-first-texture");
|
||||
assert_eq!(item.r#type, "steve");
|
||||
assert_eq!(item.size, 1);
|
||||
assert_eq!(item.hash, "abc");
|
||||
assert_eq!(item.uploader, 1);
|
||||
assert!(item.public);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modify_name() {
|
||||
let token = login();
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let body = client
|
||||
.put("http://127.0.0.1:32123/api/closet/1")
|
||||
.header("Authorization", token.clone())
|
||||
.json(&json!({"name": "renamed"}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<()>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/closet")
|
||||
.header("Authorization", token)
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Closet>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let closet = body.data().unwrap();
|
||||
let item = closet.items.get(0).unwrap();
|
||||
assert_eq!(item.tid, 1);
|
||||
assert_eq!(item.name, "renamed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_texture() {
|
||||
let token = login();
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let body = client
|
||||
.delete("http://127.0.0.1:32123/api/closet/1")
|
||||
.header("Authorization", token.clone())
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<()>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/closet")
|
||||
.header("Authorization", token)
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Closet>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let closet = body.data().unwrap();
|
||||
assert_eq!(closet.items.len(), 0);
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
#[cfg(test)]
|
||||
mod auth;
|
||||
|
||||
#[cfg(test)]
|
||||
mod closet;
|
||||
|
||||
#[cfg(test)]
|
||||
mod misc;
|
||||
|
||||
#[cfg(test)]
|
||||
mod players;
|
||||
|
||||
#[cfg(test)]
|
||||
mod types;
|
||||
|
||||
#[cfg(test)]
|
||||
mod user;
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct RootInfo {
|
||||
blessing_skin: String,
|
||||
spec: u8,
|
||||
copyright: String,
|
||||
site_name: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn api_root() {
|
||||
let body = reqwest::get("http://127.0.0.1:32123/api")
|
||||
.unwrap()
|
||||
.json::<RootInfo>()
|
||||
.unwrap();
|
||||
assert!(body.blessing_skin.starts_with("4"));
|
||||
assert_eq!(body.spec, 0);
|
||||
assert_eq!(body.copyright, "Powered with ❤ by Blessing Skin Server.");
|
||||
assert_eq!(body.site_name, "Blessing Skin");
|
||||
}
|
||||
|
|
@ -1,146 +0,0 @@
|
|||
use crate::auth::login;
|
||||
use crate::types::JsonBody;
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Player {
|
||||
pub pid: u32,
|
||||
pub name: String,
|
||||
pub tid_skin: u32,
|
||||
pub tid_cape: u32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_player() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.post("http://127.0.0.1:32123/api/players")
|
||||
.header("Authorization", login())
|
||||
.json(&json!({"name": "kotenbu_member"}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_all_players() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/players")
|
||||
.header("Authorization", login())
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Vec<Player>>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let players = body.data().unwrap();
|
||||
assert_eq!(players.len(), 1);
|
||||
|
||||
let player = players.get(0).unwrap();
|
||||
assert_eq!(player.pid, 1);
|
||||
assert_eq!(player.name, String::from("kotenbu_member"));
|
||||
assert_eq!(player.tid_skin, 0);
|
||||
assert_eq!(player.tid_cape, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modify_player_name() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.put("http://127.0.0.1:32123/api/players/1/name")
|
||||
.header("Authorization", login())
|
||||
.json(&json!({"name": "kotenbu"}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
|
||||
let player = body.data().unwrap();
|
||||
assert_eq!(player.name, String::from("kotenbu"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modify_textures() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.put("http://127.0.0.1:32123/api/players/1/textures")
|
||||
.header("Authorization", login())
|
||||
.json(&json!({"skin": 1}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let player = body.data().unwrap();
|
||||
assert_eq!(player.tid_skin, 1);
|
||||
assert_eq!(player.tid_cape, 0);
|
||||
|
||||
let body = client
|
||||
.put("http://127.0.0.1:32123/api/players/1/textures")
|
||||
.header("Authorization", login())
|
||||
.json(&json!({"cape": 2}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let player = body.data().unwrap();
|
||||
assert_eq!(player.tid_skin, 1);
|
||||
assert_eq!(player.tid_cape, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modify_textures_reset() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.delete("http://127.0.0.1:32123/api/players/1/textures")
|
||||
.header("Authorization", login())
|
||||
.json(&json!({"cape": 1}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let player = body.data().unwrap();
|
||||
assert_eq!(player.tid_skin, 1);
|
||||
assert_eq!(player.tid_cape, 0);
|
||||
|
||||
let body = client
|
||||
.delete("http://127.0.0.1:32123/api/players/1/textures")
|
||||
.header("Authorization", login())
|
||||
.json(&json!({"type": ["skin", "cape"]}))
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
let player = body.data().unwrap();
|
||||
assert_eq!(player.tid_skin, 0);
|
||||
assert_eq!(player.tid_cape, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_player() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.delete("http://127.0.0.1:32123/api/players/1")
|
||||
.header("Authorization", login())
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Player>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/players")
|
||||
.header("Authorization", login())
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<Vec<Player>>>()
|
||||
.unwrap();
|
||||
assert_eq!(body.data().unwrap().len(), 0);
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct JsonBody<T> {
|
||||
code: u8,
|
||||
data: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> JsonBody<T> {
|
||||
pub fn is_success(&self) -> bool {
|
||||
self.code == 0
|
||||
}
|
||||
|
||||
pub fn data(self) -> Option<T> {
|
||||
self.data
|
||||
}
|
||||
}
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
use crate::auth::login;
|
||||
use crate::types::JsonBody;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct User {
|
||||
pub uid: u32,
|
||||
pub email: String,
|
||||
pub nickname: String,
|
||||
pub avatar: u32,
|
||||
pub score: u32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SignResult {
|
||||
pub score: u32,
|
||||
pub storage: Usage,
|
||||
pub remaining_time: u8,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Usage {
|
||||
pub used: u32,
|
||||
pub total: u32,
|
||||
pub percentage: f32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fetch_user_info() {
|
||||
let client = reqwest::Client::new();
|
||||
let body = client
|
||||
.get("http://127.0.0.1:32123/api/user")
|
||||
.header("Authorization", login())
|
||||
.send()
|
||||
.unwrap()
|
||||
.json::<JsonBody<User>>()
|
||||
.unwrap();
|
||||
assert!(body.is_success());
|
||||
|
||||
let user = body.data().unwrap();
|
||||
assert_eq!(user.uid, 1);
|
||||
assert_eq!(user.email, String::from("ibara.mayaka@api.test"));
|
||||
assert_eq!(user.nickname, String::from("hyouka"));
|
||||
assert_eq!(user.avatar, 0);
|
||||
assert_eq!(user.score, 1000);
|
||||
}
|
||||
|
|
@ -45,6 +45,11 @@ class HomeControllerTest extends TestCase
|
|||
|
||||
public function testApiRoot()
|
||||
{
|
||||
$this->get('/api')->assertJson(['spec' => 0]);
|
||||
$this->get('/api')->assertJson([
|
||||
'blessing_skin' => config('app.version'),
|
||||
'spec' => 0,
|
||||
'copyright' => bs_copyright(),
|
||||
'site_name' => option('site_name'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user