Storyboard 01

From Alistair Mann / csi18n
Jump to: navigation, search
  1. Check a translation hasn't been uploaded
  2. Upload it
  3. Check it's present
  4. Edit it
  5. Check the edit is now present
  6. Delete it
  7. Check

Bash+cURL

#!/bin/bash
source acceptance_tests/predefined_codes.sh
 
echo "First GET of visit link should 404";
RV=$(curl -i -X GET -H 'Accept: application/json' -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you/en-CA | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "404" ]]; then
    echo ""
    echo "First GET failed :-(";
    echo $RV
    exit;
fi;
 
echo "First POST to visit link should 201 and provide location";
curl -i -X POST -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -H 'Content-Type: application/json;v=1.0' -u 'test05:test' --data '{"csi18n_xlate_resource":{"language":"en-CA","translation":"Congraturation - A winrar is you","visibility":"personal"}}' https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you >/tmp/x
RV=$(cat /tmp/x | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "201"  &&  "$RV" != "301" ]]; then
    echo ""
    echo "First POST failed :-(";
    echo $RV
    exit;
fi;
LOCATION=$(cat /tmp/x | grep "Location: " | sed 's|Location: ||' | sed 's|.$||')
if [[ "$LOCATION" == "" ]]; then
    echo ""
    echo "\nFirst POST didn't find a Location :-(\n";
    cat /tmp/x
    exit;
fi;
 
echo "Second GET to visit link should 200";
RV=$(curl -i -X GET -H 'Accept: application/json' -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you/en-CA | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "200" ]]; then
    echo ""
    echo "Second GET failed :-(\n";
    exit;
fi;
 
echo "Second GET to translation link should 200";
RV=$(curl -i -X GET -H 'Accept: application/json' -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' $LOCATION | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "200" ]]; then
    echo ""
    echo "Second GET failed :-(\n";
    exit;
fi;
 
echo "First PUT to translation link should 201";
RV=$(curl -i -X PUT -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -H 'Content-Type: application/json;v=1.0' -u 'test05:test' --data '{"csi18n_xlate_resource":{"language":"en-CA","translation":"Congraturation  A winrar is you","visibility":"personal","datetime_last_change":"2014-08-29T16:31:48.613166Z","crid":336,"sid":1,"source":"only personal upload by user"},"csi18n_xlate_location":"\/xlates\/1\/A-winner-is-you\/en-CA\/personal\/336","previous_uploads":"\/xlates\/1\/search?crid=336&page=-1&visibility=personal","later_uploads":"\/xlates\/1\/search?crid=336&page=0&visibility=personal","previous_uploads_by_ip":"\/xlates\/1\/search?crid=336&ipv4=192.168.1.64&page=-1&visibility=personal","later_uploads_by_ip":"\/xlates\/1\/search?crid=336&ipv4=192.168.1.64&page=0&visibility=personal","search_form":"\/xlates\/1\/search"}' $LOCATION | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "201" ]]; then
    echo ""
    echo "First PUT failed :-(\n";
    echo $RV
    echo $LOCATION
    exit;
fi;
 
echo "Third GET to visit link should 200 and have correction";
curl -i -X GET -H 'Accept: application/json' -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you/en-CA >/tmp/x
RV1=$(cat /tmp/x | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV1" != "200" ]]; then
    echo ""
    echo "Third GET failed :-(\n";
    echo $RV1
    exit;
fi;
RV2=$(cat /tmp/x | grep -c "Congraturation  A winrar is you")
if [[ "$RV2" != "1" ]]; then
    echo ""
    echo "Correction in Third GET not found :-(\n";
    echo $RV2
    exit;
fi;
 
echo "First DELETE to translation link should 204";
RV=$(curl -i -X DELETE -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' $LOCATION | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "204" ]]; then
    echo ""
    echo "First DELETE failed :-(\n";
    echo $RV
    exit;
fi;
 
echo "Last GET of translation link should 404";
RV=$(curl -i -X GET -H 'Accept: application/json' -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' $LOCATION | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "404" ]]; then
    echo ""
    echo "Last GET of representation failed :-(";
    echo $RV
    exit;
fi;
 
echo "Last GET of visit link should 404";
RV=$(curl -i -X GET -H 'Accept: application/json' -H 'Connection: close' -H 'X-APIKey: 798e31c43d6b9f03aa504a6f88cb4550' -u 'test05:test' https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you/en-CA | grep "HTTP/1.1" | grep -v "X-Testing-Dupe: " | cut -c 10-12)
if [[ "$RV" != "404" ]]; then
    echo ""
    echo "Last GET of representation failed :-(";
    echo $RV
    exit;
fi;
 
echo "Completed successfully"

And a sample run:

$ bash acceptance_tests/bash_storyboard_01.sh
First GET of visit link should 404
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    48  100    48    0     0     76      0 --:--:-- --:--:-- --:--:--    76
First POST to visit link should 201 and provide location
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   145  100    26  100   119    143    655 --:--:-- --:--:-- --:--:--   672
Second GET to visit link should 200
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   698  100   698    0     0   6062      0 --:--:-- --:--:-- --:--:--  6403
Second GET to translation link should 200
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   771  100   771    0     0   7517      0 --:--:-- --:--:-- --:--:--  8031
First PUT to translation link should 201
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   721  100    26  100   695    189   5061 --:--:-- --:--:-- --:--:--  5265
Third GET to visit link should 200 and have correction
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   697  100   697    0     0   6182      0 --:--:-- --:--:-- --:--:--  6514
First DELETE to translation link should 204
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Last GET of translation link should 404
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    48  100    48    0     0    484      0 --:--:-- --:--:-- --:--:--   494
Last GET of visit link should 404
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    48  100    48    0     0    421      0 --:--:-- --:--:-- --:--:--   444
Completed successfully
$

Perl

#!/usr/bin/perl -w
#
# Project_1 in Perl
# Steve Kerr, Alistair Mann
#
use warnings;
use strict;
 
#
# Set modules to use
#
use LWP::UserAgent;
use Data::Dumper;
 
#
# Constants
#
use constant VISITURL => 'https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you/en-CA';
use constant APIKEY => '798e31c43d6b9f03aa504a6f88cb4550';
use constant AUTHORISATION => 'Basic dGVzdDA1OnRlc3Q='; # base64_encode("test05:test")
use constant ACCEPTTYPE => 'application/json';
use constant CONTENTTYPE => 'application/json;v=1.0';
 
#
# HTTP Subroutines
#
# GET
sub gethttp {
    my ($url, $apikey, $authorisation, $accepttype) = @_;
 
    my $req = HTTP::Request->new(GET => $url);
    $req->header('X-APIKey' => $apikey);
    $req->header('Authorization' => $authorisation);
    $req->header('Accept' => $accepttype);
 
#    Use this code if you need to skip SSL cert checks
#
#    my $ua = LWP::UserAgent->new(ssl_opts => {
#	verify_hostname => 0,
#	SSL_verify_mode => 'SSL_VERIFY_NONE'}
#	);
#    my $res = $ua->request($req);
 
#    And this one to not skip them
    my $ua = LWP::UserAgent->new();
    my $res = $ua->request($req);
 
    return $res;
};
 
# POST
sub posthttp {
    my ($url, $apikey, $authorisation, $contenttype, $data) = @_;
 
    my $req = HTTP::Request->new(POST => $url);
    $req->header('X-APIKey' => $apikey);
    $req->header('Authorization' => $authorisation);
    $req->header('Content-Type' => $contenttype);
    $req->header('Content-Length' => length($data));
    $req->content($data);
 
    my $ua = LWP::UserAgent->new();
    my $res = $ua->request($req);
 
    return $res;
}
 
# PUT
sub puthttp {
    my ($url, $apikey, $authorisation, $contenttype, $data) = @_;
 
    my $req = HTTP::Request->new(PUT => $url);
    $req->header('X-APIKey' => $apikey);
    $req->header('Authorization' => $authorisation);
    $req->header('Content-Type' => $contenttype);
    $req->header('Content-Length' => length($data));
    $req->content($data);
 
    my $ua = LWP::UserAgent->new();
    my $res = $ua->request($req);
 
    return $res;
}
 
# DELETE
sub deletehttp {
    my ($url, $apikey, $authorisation) = @_;
 
    my $req = HTTP::Request->new(DELETE => $url);
    $req->header('X-APIKey' => $apikey);
    $req->header('Authorization' => $authorisation);
 
    my $ua = LWP::UserAgent->new();
    my $res = $ua->request($req);
 
    return $res;
};
 
#
# Main body of code
#
my $translationurl;
my $response;
 
print "First GET of visit link should 404\n";
$response = gethttp(VISITURL, APIKEY, AUTHORISATION, ACCEPTTYPE);
if ($response->code() != 404) {
    print "First GET failed :-(\n";
    print $response->code;
    print "\n";
}
 
print "First POST to visit link should 201 and provide location\n";
my $data = '{"csi18n_xlate_resource":{"language":"en-CA","translation":"Congraturation - A winrar is you","visibility":"personal"}}';
$response = posthttp("https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you", 
    APIKEY, AUTHORISATION, CONTENTTYPE, $data);
if ($response->code() != 201 && $response->code() != 301) {
    print "First POST failed :-(\n";
    print $response->code;
    print "\n";
    exit;
}
if ($response->header("Location") eq '') {
    print "First POST didn't find a Location :-(\n";
    print $response->header("Location");
    print "\n";
    exit;
}
$translationurl = $response->header("Location");
 
print "Second GET to visit link should 200\n";
$response = gethttp(VISITURL, APIKEY, AUTHORISATION, ACCEPTTYPE);
if ($response->code() != 200) {
    print "Second GET failed :-(\n";
    print $response->code;
    print "\n";
}
 
print "Second GET to translation link should 200\n";
$response = gethttp($translationurl, APIKEY, AUTHORISATION, ACCEPTTYPE);
if ($response->code() != 200) {
    print "Second GET failed :-(\n";
    print $response->code;
    print "\n";
}
 
print "First PUT to translation link should 201\n";
$data = '{"csi18n_xlate_resource":{"language":"en-CA","translation":"Congraturation  A winrar is you","visibility":"personal","datetime_last_change":"2014-08-29T16:31:48.613166Z","crid":336,"sid":1,"source":"only personal upload by user"},"csi18n_xlate_location":"\/xlates\/1\/A-winner-is-you\/en-CA\/personal\/336","previous_uploads":"\/xlates\/1\/search?crid=336&page=-1&visibility=personal","later_uploads":"\/xlates\/1\/search?crid=336&page=0&visibility=personal","previous_uploads_by_ip":"\/xlates\/1\/search?crid=336&ipv4=192.168.1.64&page=-1&visibility=personal","later_uploads_by_ip":"\/xlates\/1\/search?crid=336&ipv4=192.168.1.64&page=0&visibility=personal","search_form":"\/xlates\/1\/search"}';
 
$response = puthttp($translationurl, APIKEY, AUTHORISATION, CONTENTTYPE, $data);
if ($response->code() != 201) {
    print "First PUT failed :-(\n";
    print $response->code;
    print "\n$translationurl\n";
    exit;
}
 
print "Third GET to visit link should 200 and have correction\n";
$response = gethttp($translationurl, APIKEY, AUTHORISATION, ACCEPTTYPE);
if ($response->code() != 200) {
    print "Third GET failed :-(\n";
    print $response->code;
    print "\n";
}
my $pos = index($response->content(), "Congraturation  A winrar is you");
if ($pos == -1) {
    print "Correction in Third GET not found :-(\n";
    print "$pos\n";
}
 
print "First DELETE to translation link should 204\n";
$response = deletehttp($translationurl, APIKEY, AUTHORISATION);
if ($response->code() != 204) {
    print "First DELETE failed :-(\n";;
    print $response->code;
    print "\n";
    exit;
}
 
print "Last GET of translation link should 404\n";
$response = gethttp($translationurl, APIKEY, AUTHORISATION, ACCEPTTYPE);
if ($response->code() != 404) {
    print "Last GET of representation failed :-(\n";;
    print $response->code;
    print "\n";
}
 
print "First GET of visit link should 404\n";
$response = gethttp(VISITURL, APIKEY, AUTHORISATION, ACCEPTTYPE);
if ($response->code() != 404) {
    print "Last GET of representation failed :-(\n";;
    print $response->code;
    print "\n";
}
 
print "Completed successfully\n";

And a sample run:

$ perl acceptance_tests/perl_storyboard_01.pl
First GET of visit link should 404
First POST to visit link should 201 and provide location
Second GET to visit link should 200
Second GET to translation link should 200
First PUT to translation link should 201
Third GET to visit link should 200 and have correction
First DELETE to translation link should 204
Last GET of translation link should 404
First GET of visit link should 404
Completed successfully
$

JavaScript

JavaScript is commonly used within browsers: first some html to set up the webpage that'll track progress:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;utf-8">
 
    <script src="storyboard_01.js"></script>
  </head>
  <body>
    <div style="position: absolute; background: #BBBBBB; font-family:sans;">
 
      <input type='button' value='Start storyboard_01 test' style='font-size:30px' onClick='javascript:startTest1()'><br>
 
      Teleprinter:<br>
      <div id="teleprinter"></div>
    </div>
  </body>
</html>

... and the JavaScript itself. This is in the "storyboard_01.js" src file noted in the html ...

/*
 * Storyboard_01 in JavaScript
 *
 * Notice that running this script in a browser will generate 
 * CORS - Cross Origin Resource Sharing - requests to the server
 * that will be declined unless you've told the server about the
 * domain you're coming from. Even if the credentials are otherwise
 * okay. See techref for details.
 * 
 * Execution starts at startTest1, subject of an HTML onClick()
 * 
 */
 
//
// "Constants"
//
 
var ACCEPT = "application/json";
var AUTHORIZATION = "Basic dGVzdDA1OnRlc3Q="; // test05:test
var CONTENTTYPE = "application/json;v=1.0";
var IFMODIFIEDSINCE = "Tue,  1 Jan 1980 19:12:53 BST"; // Browser cache will otherwise interfere
var VISITLINK = 'https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you/en-CA';
var XAPIKEY = "798e31c43d6b9f03aa504a6f88cb4550";
var TLINK_STOR = 'storyboard01.translationurl'; // name of localStorage to keep created translation link
 
//
// HTTP functions
// 
 
function httpGet($url, $callback) {
    if (XMLHttpRequest) {
	var request = new XMLHttpRequest();
	if ("withCredentials" in request) {
	    request.withCredentials = false;
	    request.open('GET', $url, true);
	    request.responseType = "json";
	    request.setRequestHeader("Authorization", AUTHORIZATION);
	    request.setRequestHeader("X-APIKey", XAPIKEY);
	    request.setRequestHeader("Accept", ACCEPT);
	    request.setRequestHeader("If-Modified-Since", IFMODIFIEDSINCE);
	    request.onload = $callback;
	    request.send();
	}
    }
}
 
function httpPost($url, $callback, $representation) {
    if (XMLHttpRequest) {
	var request = new XMLHttpRequest();
	if ("withCredentials" in request) {
	    request.withCredentials = false;
	    request.open('POST', $url, true);
	    request.setRequestHeader("Authorization", AUTHORIZATION);
	    request.setRequestHeader("X-APIKey", XAPIKEY);
	    request.setRequestHeader("Content-Type", CONTENTTYPE);
	    request.onload = $callback;
	    request.send($representation);
	}
    }
}
 
function httpPut($url, $callback, $representation) {
    if (XMLHttpRequest) {
	var request = new XMLHttpRequest();
	if ("withCredentials" in request) {
	    request.withCredentials = true;
	    request.open('PUT', $url, true);
	    request.setRequestHeader("Authorization", AUTHORIZATION);
	    request.setRequestHeader("X-APIKey", XAPIKEY);
	    request.setRequestHeader("Content-Type", CONTENTTYPE);
	    request.onload = $callback;
	    request.send($representation);
	}
    }
}
 
function httpDelete($url, $callback) {
    if (XMLHttpRequest) {
	var request = new XMLHttpRequest();
	if ("withCredentials" in request) {
	    request.withCredentials = false;
	    request.open('DELETE', $url, true);
	    request.setRequestHeader("Authorization", AUTHORIZATION);
	    request.setRequestHeader("X-APIKey", XAPIKEY);
	    request.setRequestHeader("If-Modified-Since", IFMODIFIEDSINCE);
	    request.onload = $callback;
	    request.send();
	}
    }
}
 
//
// Teleprinting washboard 
//
 
function printup($msg) {
    var d = document.getElementById("teleprinter");
    d.innerHTML = d.innerHTML + $msg + "<br>";
}
 
//
// Main action. Each function below calls the relevant HTTP function above
// and includes within it the next function to call once a result is available.
//
 
function startTest1() {
    printup("First GET of visit link should 404");
    httpGet(VISITLINK, replyTest1StartTest2);
}
 
function replyTest1StartTest2() {
    if (this.status != 404) {
	printup("First GET failed :-(");
	printup(this.status);
	printup(this.getResponseHeader('Content-Location'));
 
    } else {
	printup("First POST to visit link should 201 and provide location");
	$data = '{"csi18n_xlate_resource":{"language":"en-CA","translation":"Congraturation - A winrar is you","visibility":"personal"}}';
	httpPost('https://rest.mpsvr.com:443/newmarks/1/A-winner-is-you', replyTest2StartTest3, $data);
    } 
}
 
function replyTest2StartTest3() {
    if (this.status != 201 && this.status != 301) {
	printup("First POST failed :-(");
	printup(this.status);
 
    } else {
	// localStorage persists the URL of the created resource, everywhere
	// needs it
	var $location = this.getResponseHeader('Location');
	localStorage.setItem(TLINK_STOR, $location);
	printup($location);
 
	printup("Second GET of visit link should 200");
	httpGet(VISITLINK, replyTest3StartTest4);
    } 
}
 
function replyTest3StartTest4() {
    if (this.status != 200) {
	printup("Second GET failed :-(");
	printup(this.status);
 
    } else {
	var $location = localStorage.getItem(TLINK_STOR);
	printup("Second GET of translation link should 200");
	httpGet($location, replyTest4StartTest5);
    } 
}
 
function replyTest4StartTest5() {
    if (this.status != 200) {
	printup("Second GET failed :-(");
	printup(this.status);
 
    } else {
	var $json = this.response;
	$json.csi18n_xlate_resource.translation = "Congraturation  A winrar is you";
	$representation = JSON.stringify($json);
 
	var $location = localStorage.getItem(TLINK_STOR);
	printup("First PUT to translation link should 201");
	httpPut($location, replyTest5StartTest6, $representation);
    } 
}
 
function replyTest5StartTest6() {
    if (this.status != 201) {
	printup("First PUT failed :-(");
	printup(this.status);
 
    } else {
	printup("Third GET of visit link should 200 and have correction");
	httpGet(VISITLINK, replyTest6StartTest7);
    } 
}
 
function replyTest6StartTest7() {
    if (this.status != 200) {
	printup("Third GET failed :-(");
	printup(this.status);
 
    } else {
	var $json = this.response;
	if ($json.csi18n_xlate_resource.translation != "Congraturation  A winrar is you") {
	    printup("Correction in Third GET not found :-(");
	}
 
	var $location = localStorage.getItem(TLINK_STOR);
	printup("First DELETE to translation link should 204");
	httpDelete($location, replyTest7StartTest8);
    } 
}
 
function replyTest7StartTest8() {
    if (this.status != 204) {
	printup("First DELETE failed :-(");
	printup(this.status);
 
    } else {
	var $location = localStorage.getItem(TLINK_STOR);
	printup("Last GET of translation link should 404");
	httpGet($location, replyTest8StartTest9);
    } 
}
 
function replyTest8StartTest9() {
    if (this.status != 404) {
	printup("Last GET of representation failed :-(");
	printup(this.status);
 
    } else {
	printup("First GET of visit link should 404");
	httpGet(VISITLINK, replyTest9);
    } 
}
 
function replyTest9() {
    if (this.status != 404) {
	printup("Last GET of representation failed :-(");
	printup(this.status);
 
    } else {
	printup("Completed successfully");
    }
}

... And a screen grab from a sample run:
Javascript-storyboard 01-result-grab.png

PHP