From fe5b7c73802fa7aef8473e00391cf738f7557e38 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sat, 27 Apr 2019 20:08:41 +0800 Subject: [PATCH] Add more API tests --- Cargo.lock | 53 ++++++++++++++++ Cargo.toml | 1 + app/Http/Controllers/PlayerController.php | 7 ++- tests/Api/tests/players.rs | 76 +++++++++++++++++++++++ 4 files changed, 135 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5437dd28..a7a8260f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -247,6 +247,16 @@ dependencies = [ "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "flate2" version = "1.0.7" @@ -432,6 +442,20 @@ name = "libc" version = "0.2.51" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libsqlite3-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "linked-hash-map" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lock_api" version = "0.1.5" @@ -449,6 +473,14 @@ dependencies = [ "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lru-cache" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "matches" version = "0.1.8" @@ -872,6 +904,20 @@ dependencies = [ "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rusqlite" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fallible-streaming-iterator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", + "libsqlite3-sys 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc-demangle" version = "0.1.14" @@ -1043,6 +1089,7 @@ dependencies = [ "openssl 0.10.20 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.9.43 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rusqlite 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1354,6 +1401,8 @@ dependencies = [ "checksum error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "07e791d3be96241c77c43846b665ef1384606da2cd2a48730abe606a12906e02" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" +"checksum fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" +"checksum fallible-streaming-iterator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" "checksum flate2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f87e68aa82b2de08a6e037f1385455759df6e445a8df5e005b4297191dbf18aa" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" @@ -1376,8 +1425,11 @@ dependencies = [ "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" "checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" +"checksum libsqlite3-sys 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e310445ab028c374b9efaaed4b7a52a14e3b8ad5a1351b4bbd46dec03ffce717" +"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" "checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" @@ -1422,6 +1474,7 @@ dependencies = [ "checksum regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcfd8681eebe297b81d98498869d4aae052137651ad7b96822f09ceb690d0a96" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" "checksum reqwest 0.9.15 (registry+https://github.com/rust-lang/crates.io-index)" = "943b9f85622f53bcf71721e0996f23688e3942e51fc33766c2e24a959316767b" +"checksum rusqlite 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "700720c977deb8b91c9d881dcbe3309c254d414078ca3856ea6647e569be3b66" "checksum rustc-demangle 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" diff --git a/Cargo.toml b/Cargo.toml index e4ebbc65..2c340427 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ doctest = false [dev-dependencies] reqwest = "0.9" +rusqlite = "0.18" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/app/Http/Controllers/PlayerController.php b/app/Http/Controllers/PlayerController.php index 020ef2c5..2b0e8d74 100644 --- a/app/Http/Controllers/PlayerController.php +++ b/app/Http/Controllers/PlayerController.php @@ -160,11 +160,14 @@ class PlayerController extends Controller { $player = Player::find($pid); array_map(function ($type) use ($request, $player) { - if ($request->has($type)) { + if ( + $request->has($type) || + ($request->has('type') && in_array($type, $request->input('type'))) + ) { $field = "tid_$type"; $player->$field = 0; } - }, $request->input('type') ?? ['skin', 'cape']); + }, ['skin', 'cape']); $player->save(); return json(trans('user.player.clear.success', ['name' => $player->name]), 0, $player->toArray()); diff --git a/tests/Api/tests/players.rs b/tests/Api/tests/players.rs index 8862866a..56481793 100644 --- a/tests/Api/tests/players.rs +++ b/tests/Api/tests/players.rs @@ -1,7 +1,9 @@ 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 Player { @@ -63,6 +65,80 @@ fn modify_player_name() { assert_eq!(player.name, String::from("kotenbu")); } +#[test] +fn modify_textures() { + 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 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::>() + .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::>() + .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::>() + .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::>() + .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();