Skip to content

Commit

Permalink
Move create wall API to new REST structure
Browse files Browse the repository at this point in the history
  • Loading branch information
birtles committed Mar 15, 2013
1 parent 24138ce commit 465e400
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 185 deletions.
3 changes: 2 additions & 1 deletion wall/public/api/.htaccess
Expand Up @@ -9,5 +9,6 @@ Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /api/ 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> </ifModule>
59 changes: 59 additions & 0 deletions wall/public/api/doWalls.php
@@ -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);
?>
29 changes: 0 additions & 29 deletions wall/public/api/getWall.php

This file was deleted.

41 changes: 0 additions & 41 deletions wall/public/wall-maker/api/createWall.php

This file was deleted.

2 changes: 1 addition & 1 deletion wall/public/wall-maker/js/create-wall.js
Expand Up @@ -39,7 +39,7 @@ var CreateWallController =
Navigation.showScreen("screen-new"); Navigation.showScreen("screen-new");
// Send request // Send request
var payload = CreateWallForm.getFormValues(); var payload = CreateWallForm.getFormValues();
ParaPara.postUrl(WallMaker.rootUrl + '/api/createWall', payload, ParaPara.postUrl('/api/walls', payload,
this.createSuccess.bind(this), this.createSuccess.bind(this),
this.createError.bind(this)); this.createError.bind(this));
}, },
Expand Down
54 changes: 54 additions & 0 deletions wall/tests/api/TestCreateWall.php
@@ -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??)
}

?>
24 changes: 6 additions & 18 deletions wall/tests/api/TestGetWall.php
Expand Up @@ -14,11 +14,10 @@ function __construct($name = false) {
} }


function testLoggedOut() { function testLoggedOut() {
$wallId = $this->createWall( // Create wall
array('ownerEmail' => $this->userEmail, $this->login();
'design' => $this->testDesignId, $wallId = $this->createWall('Test wall', $this->testDesignId);
'title' => 'Test wall') $this->logout();
);


// Check it fails if we're logged out // Check it fails if we're logged out
$wall = $this->getWall($wallId); $wall = $this->getWall($wallId);
Expand All @@ -34,14 +33,8 @@ function testLoggedOut() {


function testGetWall() { function testGetWall() {
// Create wall // Create wall
$wallId = $this->createWall(
array('ownerEmail' => $this->userEmail,
'design' => $this->testDesignId,
'title' => 'Test wall')
);

// Login
$this->login(); $this->login();
$wallId = $this->createWall('Test wall', $this->testDesignId);


// Check it succeeds // Check it succeeds
$wall = $this->getWall($wallId); $wall = $this->getWall($wallId);
Expand Down Expand Up @@ -86,13 +79,8 @@ function looksLikeAUrl($url) {
return @$parts['scheme'] == 'http' || return @$parts['scheme'] == 'http' ||
@$parts['scheme'] == 'https'; @$parts['scheme'] == 'https';
} }

function getWall($wallId) {
// Set cookie
if ($this->sessionId) {
$this->setCookie(WALLMAKER_SESSION_NAME, session_id());
}


function getWall($wallId) {
// Make request // Make request
global $config; global $config;
$url = $config['test']['wall_server'] . 'api/walls/' . $wallId; $url = $config['test']['wall_server'] . 'api/walls/' . $wallId;
Expand Down
48 changes: 0 additions & 48 deletions wall/tests/api/TestSessions.php
Expand Up @@ -223,11 +223,6 @@ function checkSession($session, $shouldBeOpen) {
} }


function getWall($wallId) { function getWall($wallId) {
// Set cookie
if ($this->sessionId) {
$this->setCookie(WALLMAKER_SESSION_NAME, session_id());
}

// Make request // Make request
global $config; global $config;
$url = $config['test']['wall_server'] . 'api/walls/' . $wallId; $url = $config['test']['wall_server'] . 'api/walls/' . $wallId;
Expand All @@ -249,45 +244,7 @@ function getWall($wallId) {
return $wall; 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) { function closeSession($wallId, $sessionId) {
// Set cookie
if ($this->sessionId) {
$this->setCookie(WALLMAKER_SESSION_NAME, session_id());
}

// Prepare payload // Prepare payload
$payload['wallId'] = $wallId; $payload['wallId'] = $wallId;
$payload['sessionId'] = $sessionId; $payload['sessionId'] = $sessionId;
Expand All @@ -310,11 +267,6 @@ function closeSession($wallId, $sessionId) {
} }


function startNewSession($wallId, $sessionId) { function startNewSession($wallId, $sessionId) {
// Set cookie
if ($this->sessionId) {
$this->setCookie(WALLMAKER_SESSION_NAME, session_id());
}

// Prepare payload // Prepare payload
$payload['wallId'] = $wallId; $payload['wallId'] = $wallId;
$payload['sessionId'] = $sessionId; $payload['sessionId'] = $sessionId;
Expand Down
11 changes: 1 addition & 10 deletions wall/tests/api/TestUserSummary.php
Expand Up @@ -46,11 +46,7 @@ function testWalls() {
count($summary['walls']) === 0, count($summary['walls']) === 0,
"Should have got an empty list of walls"); "Should have got an empty list of walls");


$wallId = $this->createWall( $wallId = $this->createWall('Test wall', $this->testDesignId);
array('ownerEmail' => $this->userEmail,
'design' => $this->testDesignId,
'title' => 'Test wall')
);
$summary = $this->getMySummary(); $summary = $this->getMySummary();
$this->assertTrue(count($summary['walls']) === 1, $this->assertTrue(count($summary['walls']) === 1,
"Should have got one wall back"); "Should have got one wall back");
Expand Down Expand Up @@ -107,11 +103,6 @@ function testDesigns() {
} }


function getMySummary() { function getMySummary() {
// Set cookie
if ($this->sessionId) {
$this->setCookie(WALLMAKER_SESSION_NAME, session_id());
}

// Make request // Make request
global $config; global $config;
$url = $config['test']['wall_server'] . 'api/userSummary'; $url = $config['test']['wall_server'] . 'api/userSummary';
Expand Down

0 comments on commit 465e400

Please sign in to comment.