Permalink
Browse files

Move create wall API to new REST structure

  • Loading branch information...
birtles committed Mar 15, 2013
1 parent 24138ce commit 465e4003870582f464cb5eac407dc7fc1f63c1f0
@@ -9,5 +9,6 @@ Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /api/
- RewriteRule ^walls/([0-9]+)$ getWall.php?id=$1 [L]
+ RewriteRule ^walls$ doWalls.php [L]
+ RewriteRule ^walls/([0-9]+)$ doWalls.php?id=$1 [L]
</ifModule>
@@ -0,0 +1,59 @@
+<?php
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+require_once('../../lib/parapara.inc');
+require_once('api.inc');
+require_once('walls.inc');
+
+header('Content-Type: text/plain; charset=UTF-8');
+
+// Check we are logged in
+session_name(WALLMAKER_SESSION_NAME);
+session_start();
+if (!isset($_SESSION['email'])) {
+ bailWithError('logged-out');
+}
+
+// Prepare common parameters
+$wallId = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : null;
+
+// Parse input
+$handle = fopen('php://input','r');
+$jsonString = fgets($handle);
+$json = json_decode($jsonString,true);
+fclose($handle);
+
+switch ($_SERVER['REQUEST_METHOD']) {
+ case 'POST':
+ $params = array('ownerEmail' => $_SESSION['email']);
+ $params['design'] = @$json['design'];
+ $params['title'] = @$json['title'];
+
+ // XXX Move the following to Walls::create
+ // Run the query
+ $wallId = createWall($params);
+
+ // Prepare result
+ $result = array('wallId' => $wallId);
+ // start session
+ $currentdatetime = gmdate("Y-m-d H:i:s");
+ startNewSession($wallId, null, $currentdatetime);
+ break;
+
+ case 'GET':
+ if ($wallId === null) {
+ // XXX Return the list of walls here
+ bailWithError('bad-request');
+ }
+ $result = getWallDetails($wallId, $_SESSION['email']);
+ break;
+
+ default:
+ bailWithError('bad-request');
+}
+
+// Return the result
+print json_encode($result);
+?>
@@ -1,29 +0,0 @@
-<?php
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-require_once('../../lib/parapara.inc');
-require_once('api.inc');
-require_once('walls.inc');
-
-header('Content-Type: text/plain; charset=UTF-8');
-
-// Check we are logged in
-session_name(WALLMAKER_SESSION_NAME);
-session_start();
-if (!isset($_SESSION['email'])) {
- bailWithError('logged-out');
-}
-
-// Prepare parameters
-$wallId = @$_REQUEST['id'];
-if (!isset($wallId)) {
- bailWithError('no-wall');
-}
-
-$wall = getWallDetails($wallId, $_SESSION['email']);
-
-// Return the result
-print json_encode($wall);
-?>
@@ -1,41 +0,0 @@
-<?php
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-require_once('../../../lib/parapara.inc');
-require_once('api.inc');
-require_once('walls.inc');
-
-header('Content-Type: text/plain; charset=UTF-8');
-
-// Check we are logged in
-session_name(WALLMAKER_SESSION_NAME);
-session_start();
-if (!isset($_SESSION['email'])) {
- bailWithError('logged-out');
-}
-
-// Parse input
-$handle = fopen('php://input','r');
-$jsonString = fgets($handle);
-$json = json_decode($jsonString,true);
-fclose($handle);
-
-// Prepare parameters
-$params = array('ownerEmail' => $_SESSION['email']);
-$params['design'] = @$json['design'];
-$params['title'] = @$json['title'];
-
-// Run the query
-$wallId = createWall($params);
-
-// Prepare result
-$result = array('wallId' => $wallId);
-// start session
-$currentdatetime = gmdate("Y-m-d H:i:s");
-startNewSession($wallId, null, $currentdatetime);
-
-// Return the result
-print json_encode($result);
-?>
@@ -39,7 +39,7 @@ var CreateWallController =
Navigation.showScreen("screen-new");
// Send request
var payload = CreateWallForm.getFormValues();
- ParaPara.postUrl(WallMaker.rootUrl + '/api/createWall', payload,
+ ParaPara.postUrl('/api/walls', payload,
this.createSuccess.bind(this),
this.createError.bind(this));
},
@@ -0,0 +1,54 @@
+<?php
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+require_once('../../lib/parapara.inc');
+require_once('simpletest/autorun.php');
+require_once('WallMakerTestCase.php');
+
+class CreateWallTestCase extends WallMakerTestCase {
+
+ function __construct($name = false) {
+ parent::__construct($name);
+ }
+
+ function testLoggedOut() {
+ // Check it fails if we're logged out
+ $wall = $this->_createWall(
+ 'Test wall',
+ $this->testDesignId
+ );
+ $this->assertTrue(@$wall['error_key'] == 'logged-out',
+ "Got wall whilst logged out.");
+ }
+
+ function testCreate() {
+ $this->login();
+
+ // Create wall
+ $wall = $this->_createWall(
+ 'Test wall',
+ $this->testDesignId
+ );
+ $this->assertTrue(!array_key_exists('error_key', $wall),
+ "Got error creating wall");
+ $this->assertTrue(is_int(@$wall['wallId']) && $wall['wallId'] > 0,
+ "Unexpected wall ID: " . @$wall['wallId']);
+
+ // Tidy up
+ $this->removeWall($wall['wallId']);
+ }
+
+ // Test creating works (get ID)
+ // Test PUT method doesn't work?
+ // Test name
+ // -- no name is error
+ // -- whitespace
+ // Test designId
+ // Test session exists
+ // Test wall name is set
+ // (I think it should basically return the same as fetching the wall??)
+}
+
+?>
@@ -14,11 +14,10 @@ function __construct($name = false) {
}
function testLoggedOut() {
- $wallId = $this->createWall(
- array('ownerEmail' => $this->userEmail,
- 'design' => $this->testDesignId,
- 'title' => 'Test wall')
- );
+ // Create wall
+ $this->login();
+ $wallId = $this->createWall('Test wall', $this->testDesignId);
+ $this->logout();
// Check it fails if we're logged out
$wall = $this->getWall($wallId);
@@ -34,14 +33,8 @@ function testLoggedOut() {
function testGetWall() {
// Create wall
- $wallId = $this->createWall(
- array('ownerEmail' => $this->userEmail,
- 'design' => $this->testDesignId,
- 'title' => 'Test wall')
- );
-
- // Login
$this->login();
+ $wallId = $this->createWall('Test wall', $this->testDesignId);
// Check it succeeds
$wall = $this->getWall($wallId);
@@ -86,13 +79,8 @@ function looksLikeAUrl($url) {
return @$parts['scheme'] == 'http' ||
@$parts['scheme'] == 'https';
}
-
- function getWall($wallId) {
- // Set cookie
- if ($this->sessionId) {
- $this->setCookie(WALLMAKER_SESSION_NAME, session_id());
- }
+ function getWall($wallId) {
// Make request
global $config;
$url = $config['test']['wall_server'] . 'api/walls/' . $wallId;
@@ -223,11 +223,6 @@ function checkSession($session, $shouldBeOpen) {
}
function getWall($wallId) {
- // Set cookie
- if ($this->sessionId) {
- $this->setCookie(WALLMAKER_SESSION_NAME, session_id());
- }
-
// Make request
global $config;
$url = $config['test']['wall_server'] . 'api/walls/' . $wallId;
@@ -249,45 +244,7 @@ function getWall($wallId) {
return $wall;
}
- function createWall($title, $designId) {
- // XXX We will define this globally once this API is tidied up
-
- // Set cookie
- if ($this->sessionId) {
- $this->setCookie(WALLMAKER_SESSION_NAME, session_id());
- }
-
- // Prepare payload
- $payload['title'] = $title;
- $payload['design'] = $designId;
-
- // Make request
- global $config;
- $url = $config['test']['wall_server'] . 'wall-maker/api/createWall';
- $response = $this->post($url, json_encode($payload));
-
- // Check response
- $this->assertResponse(200);
- $this->assertMime('text/plain; charset=UTF-8');
-
- // Parse response
- $wall = json_decode($response,true);
- $this->assertTrue($wall !== null,
- "Failed to decode response: $response");
-
- // Check there's no error
- $this->assertTrue(!array_key_exists('error_key', $wall),
- "Failed to get wall:" . @$wall['error_key']);
-
- return $wall['wallId'];
- }
-
function closeSession($wallId, $sessionId) {
- // Set cookie
- if ($this->sessionId) {
- $this->setCookie(WALLMAKER_SESSION_NAME, session_id());
- }
-
// Prepare payload
$payload['wallId'] = $wallId;
$payload['sessionId'] = $sessionId;
@@ -310,11 +267,6 @@ function closeSession($wallId, $sessionId) {
}
function startNewSession($wallId, $sessionId) {
- // Set cookie
- if ($this->sessionId) {
- $this->setCookie(WALLMAKER_SESSION_NAME, session_id());
- }
-
// Prepare payload
$payload['wallId'] = $wallId;
$payload['sessionId'] = $sessionId;
@@ -46,11 +46,7 @@ function testWalls() {
count($summary['walls']) === 0,
"Should have got an empty list of walls");
- $wallId = $this->createWall(
- array('ownerEmail' => $this->userEmail,
- 'design' => $this->testDesignId,
- 'title' => 'Test wall')
- );
+ $wallId = $this->createWall('Test wall', $this->testDesignId);
$summary = $this->getMySummary();
$this->assertTrue(count($summary['walls']) === 1,
"Should have got one wall back");
@@ -107,11 +103,6 @@ function testDesigns() {
}
function getMySummary() {
- // Set cookie
- if ($this->sessionId) {
- $this->setCookie(WALLMAKER_SESSION_NAME, session_id());
- }
-
// Make request
global $config;
$url = $config['test']['wall_server'] . 'api/userSummary';
Oops, something went wrong.

0 comments on commit 465e400

Please sign in to comment.