Changeset 519df6e for src/webservice


Ignore:
Timestamp:
Aug 30, 2015, 8:58:06 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
a0f5395
Parents:
d5d4498
Message:

Proceeded in implementing /stations/register function

Location:
src/webservice
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/htdocs/index.php

    rd5d4498 r519df6e  
    4949                $app = \Slim\Slim::getInstance("siweather");
    5050                $body = $app->request->getBody();
    51                 $json = json_decode($body);
     51                $json = json_decode($body, true);
    5252
    53                 $location = new IpGeoLocator($app->request->getIp());
    54                 if($location->city() != $json["city"]){
    55                         return json_encode(["result" => "unprovenlocation", "message" => "You send "]);
     53                $app->response()->header("Content-Type", "application/json");
     54
     55                $expectedKeys = array("city", "longitude", "latitude");
     56                $missingKeys = array();
     57                foreach($expectedKeys as $key){
     58                        if(!array_key_exists($key, $json))
     59                                $missingKeys[] = $key;
    5660                }
    57                
    58                 $app->response()->header("Content-Type", "application/json");
     61                if(count($missingKeys) > 0){
     62                        echo json_encode
     63                        (
     64                                [
     65                                        "result" => "missingkeys",
     66                                        "message" => "Your request misses the following keys: ". implode(", ", $missingKeys),
     67                                ]
     68                        );
     69                        return;
     70                }
     71
     72                if(array_key_exists("locationchecked", $json) && $json["locationchecked"]){
     73                        // user has checked the location already and we add it now
     74                       
     75                }
     76
     77                $errorMessage = "";
     78                $location = SiWeather\Webservice\IpGeoLocator::locate($app->request->getIp(), $errorMessage);
     79                if(is_null($location)){
     80                        error_log($errorMessage);
     81                        echo json_encode
     82                        (
     83                                [
     84                                        "result" => "unprovenlocation",
     85                                        "message" => "Could not check the city (". $json["city"] .") and location (". $json["longitude"] .", ". $json["latitude"] .") you send with help of your ip address. Please make sure, your data is correct. If your data is correct, please resend your request with the keyword \"locationchecked = true\".",
     86                                ]
     87                        );
     88                        return;
     89                }
     90                elseif($location->city() != $json["city"]){
     91                        echo json_encode
     92                        (
     93                                [
     94                                        "result" => "unprovenlocation",
     95                                        "message" => "The city (". $json[city] .") and location (". $json["longitude"] .", ". $json["latitude"] .") you send, does not suit with your ip address associtated location (city: ". $location->city() ."; Location: ". $location->longitude() .", ". $location->latitude() ."). If your data is correct, please resend your request with the keyword \"locationchecked = true\".",
     96                                ]
     97                        );
     98                        return;
     99                }
     100
    59101                echo json_encode($json);
    60102        }
  • src/webservice/tests/StationsTest.php

    rd5d4498 r519df6e  
    6565                        ]
    6666                );
     67
     68                // sending must failing register request, caused by unproven location
    6769                $response = $client->post("index.php/stations/register",
    68                         [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Krefeld"]]
     70                        [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town"]]
    6971                );
    70                 echo $response->getStatusCode();
    71                 echo $response->getBody();
     72                $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
     73                $json = json_decode($response->getBody(), true);
     74                $this->assertTrue(array_key_exists("result", $json));
     75                $this->assertTrue(array_key_exists("message", $json));
     76                $this->assertEquals("unprovenlocation", $json["result"]);
     77               
     78                // try again with set "locationchecked" to true
     79                $response = $client->post("index.php/stations/register",
     80                        [ "json" => ["name" => "Cool Station", "longitude" => 23.2,
     81                                                                         "latitude" => 90, "city" => "Nice town", "locationchecked" => true]]
     82                );
     83                $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
     84                $json = json_decode($response->getBody(), true);
     85                $this->assertTrue(array_key_exists("result", $json));
     86                $this->assertTrue(array_key_exists("uuid", $json));
     87                $this->assertEquals("success", $json["result"]);
     88                $this->assertTrue(!empty($json["uuid"]));
     89
    7290        }
    7391       
    … …  
    91109                $body = $response->getBody();
    92110                $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
    93                 $json = json_decode($body);
     111                $json = json_decode($body, true);
    94112                $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
    95113                return $json;
Note: See TracChangeset for help on using the changeset viewer.