-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathversion.rs
72 lines (64 loc) · 2.48 KB
/
version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::collections::HashMap;
use rustc_serialize::json::Json;
use conduit::{Handler, Method};
use semver;
use cargo_registry::db::RequestTransaction;
use cargo_registry::version::{EncodableVersion, Version};
#[derive(RustcDecodable)]
struct VersionList { versions: Vec<EncodableVersion> }
#[derive(RustcDecodable)]
struct VersionResponse { version: EncodableVersion }
fn sv(s: &str) -> semver::Version {
semver::Version::parse(s).unwrap()
}
#[test]
fn index() {
let (_b, app, middle) = ::app();
let mut req = ::req(app, Method::Get, "/api/v1/versions");
let mut response = ok_resp!(middle.call(&mut req));
let json: VersionList = ::json(&mut response);
assert_eq!(json.versions.len(), 0);
let (v1, v2) = {
::mock_user(&mut req, ::user("foo"));
let (c, _) = ::mock_crate(&mut req, ::krate("foo_vers_index"));
let tx = req.tx().unwrap();
let m = HashMap::new();
let v1 = Version::insert(tx, c.id, &sv("2.0.0"), &m, &[]).unwrap();
let v2 = Version::insert(tx, c.id, &sv("2.0.1"), &m, &[]).unwrap();
(v1, v2)
};
req.with_query(&format!("ids[]={}&ids[]={}", v1.id, v2.id));
let mut response = ok_resp!(middle.call(&mut req));
let json: VersionList = ::json(&mut response);
assert_eq!(json.versions.len(), 2);
}
#[test]
fn show() {
let (_b, app, middle) = ::app();
let mut req = ::req(app.clone(), Method::Get, "/api/v1/versions");
let v = {
let conn = app.diesel_database.get().unwrap();
let user = ::new_user("foo").create_or_update(&conn).unwrap();
let krate = ::CrateBuilder::new("foo_vers_show", user.id)
.expect_build(&conn);
::new_version(krate.id, "2.0.0").save(&conn, &[]).unwrap()
};
req.with_path(&format!("/api/v1/versions/{}", v.id));
let mut response = ok_resp!(middle.call(&mut req));
let json: VersionResponse = ::json(&mut response);
assert_eq!(json.version.id, v.id);
}
#[test]
fn authors() {
let (_b, app, middle) = ::app();
let mut req = ::req(app, Method::Get, "/api/v1/crates/foo_authors/1.0.0/authors");
::mock_user(&mut req, ::user("foo"));
::mock_crate(&mut req, ::krate("foo_authors"));
let mut response = ok_resp!(middle.call(&mut req));
let mut data = Vec::new();
response.body.write_body(&mut data).unwrap();
let s = ::std::str::from_utf8(&data).unwrap();
let json = Json::from_str(&s).unwrap();
let json = json.as_object().unwrap();
assert!(json.contains_key(&"users".to_string()));
}