Browse Source

check page

master
Robin Thoni 9 years ago
parent
commit
e29a14f006
3 changed files with 127 additions and 1 deletions
  1. 2
    1
      README
  2. 117
    0
      misc/check.php
  3. 8
    0
      misc/clear_database.sql

+ 2
- 1
README View File

@@ -29,7 +29,8 @@ Database structure:
29 29
   Create database, then insert this structure
30 30
 
31 31
 API test:
32
-  Just create database structure
32
+  Create database structure
33
+  Clear database with misc/clear_database.sql before testing
33 34
   Browse misc/check.php
34 35
 
35 36
 Clear script:

+ 117
- 0
misc/check.php View File

@@ -0,0 +1,117 @@
1
+<?php
2
+$host = "https://api.vdm.rthoni.com/";
3
+$key = "demo42";
4
+
5
+header("Content-Type: text/plain");
6
+
7
+$token = "";
8
+function error($http)
9
+{
10
+  echo "[ KO ] ";
11
+  var_dump($http);
12
+  die();
13
+}
14
+
15
+function check_error($http)
16
+{
17
+  if ($http->responseCode != 200)
18
+    error($http);
19
+  return json_decode($http->body);
20
+}
21
+
22
+function api_post($url, $param)
23
+{
24
+  global $host;
25
+  global $key;
26
+  global $token;
27
+  $http = http_post_fields($host . $url, $param, array(), array("headers" =>
28
+    array("Api-Key" => $key, "Authorization" => $token)));
29
+  return check_error(http_parse_message($http));
30
+}
31
+
32
+function api_get($url, $param)
33
+{
34
+  global $host;
35
+  global $key;
36
+  global $token;
37
+  $f = true;
38
+  foreach ($param as $k => $v)
39
+  {
40
+    if ($f)
41
+    {
42
+      $f = false;
43
+      $url .= "?";
44
+    }
45
+    else
46
+      $url .= "&";
47
+    $url .= urlencode($k) . "=" . urlencode($v);
48
+  }
49
+  $http = http_get($host . $url, array("headers" =>
50
+    array("Api-Key" => $key, "Authorization" => $token)));
51
+  return check_error(http_parse_message($http));
52
+}
53
+
54
+function add_status($status, $long, $lat, $r)
55
+{
56
+  $params = array("status" => $status);
57
+  if ($long !== null)
58
+    $params["longitude"] = $long;
59
+  if ($lat !== null)
60
+    $params["latitude"] = $lat;
61
+  $s = api_post("status", $params);
62
+  if ($s->user_id != $r->id)
63
+    die("[ KO ] Users ids don't match");
64
+  if (($long !== null && $s->longitude != $long)
65
+    || ($long === null && $s->longitude != null))
66
+    die("[ KO ] Longitudes don't match");
67
+  if (($lat !== null && $s->latitude != $lat)
68
+    || ($lat === null && $s->latitude != null))
69
+    die("[ KO ] Latitudes don't match");
70
+
71
+  echo "[ OK ] Status added\n";
72
+}
73
+
74
+function mk_status($status, $long, $lat)
75
+{
76
+  return array("status" => $status,
77
+    "long" => $long,
78
+    "lat" => $lat);
79
+}
80
+
81
+$username = "check_" . rand();
82
+$password = md5(rand());
83
+$user_pass = array("username" => $username, "password" => $password);
84
+
85
+
86
+$r = api_post("user", $user_pass);
87
+echo "[ OK ] User " . $r->username . " (" . $r->id . ") created\n";
88
+
89
+$l = api_post("user/login", $user_pass);
90
+if ($l->username != $r->username)
91
+  die("[ KO ] Usernames don't match");
92
+if ($l->id != $r->id)
93
+  die("[ KO ] Users ids don't match");
94
+$token = $r->token;
95
+echo "[ OK ] User " . $r->username . " (" . $r->id . ") logged in\n";
96
+
97
+$status = array(mk_status("check status null null", null, null),
98
+  mk_status("check status 42 null", 42, null),
99
+  mk_status("check status null 42", null, 42),
100
+  mk_status("check status 42.42 42", 42.42, 42));
101
+
102
+foreach ($status as $st)
103
+  add_status($st['status'], $st['long'], $st['lat'], $r);
104
+
105
+$f = api_get("status/feed", array());
106
+if (count($f) != count($status))
107
+  die("[ KO ] Did not found " . count($status).
108
+    " status (Was the database cleared?)\n");
109
+
110
+echo "[ OK ] All the status were added\n";
111
+
112
+api_post("status/" . $f[0]->id . "/confirm", array("confirmation" => 1));
113
+echo "[ OK ] Status confirmed once\n";
114
+api_post("status/" . $f[0]->id . "/confirm", array("confirmation" => 0));
115
+echo "[ OK ] Status confirmed tiwce\n";
116
+
117
+?>

+ 8
- 0
misc/clear_database.sql View File

@@ -0,0 +1,8 @@
1
+TRUNCATE confirms;
2
+ALTER TABLE confirms AUTO_INCREMENT = 1;
3
+TRUNCATE status;
4
+ALTER TABLE status AUTO_INCREMENT = 1;
5
+TRUNCATE tokens;
6
+ALTER TABLE tokens AUTO_INCREMENT = 1;
7
+TRUNCATE users;
8
+ALTER TABLE users AUTO_INCREMENT = 1;

Loading…
Cancel
Save