/v1/get sunshine.
[pin4sha_cgi.git] / tests / test-post.sh
blobe981c0ae5c52905beb35144821f0d46064e59fba
1 #!/bin/sh
3 # Copyright (c) 2015-2016 Marcus Rohrmoser http://mro.name/me. All rights reserved.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 cd "$(dirname "$0")/../tmp"
19 . ../scripts/assert.sh
21 # Check preliminaries
22 curl --version >/dev/null || assert_fail 101 "I need curl."
23 xmllint --version 2> /dev/null || assert_fail 102 "I need xmllint (libxml2)."
24 ruby --version > /dev/null || assert_fail 103 "I need ruby."
25 [ "${USERNAME}" != "" ] || assert_fail 1 "How strange, USERNAME is unset."
26 [ "${PASSWORD}" != "" ] || assert_fail 2 "How strange, PASSWORD is unset."
27 [ "${BASE_URL}" != "" ] || assert_fail 3 "How strange, BASE_URL is unset."
28 assert_equal "" "$(echo "${BASE_URL}" | egrep -e "/$")" 28 "BASE_URL must be without trailing /"
30 echo "###################################################"
31 echo "## Non-logged-in Atom feed before adding a link (should have only the initial public default entry):"
32 curl --silent --show-error --output curl.tmp.atom "${BASE_URL}/?do=atom"
33 xmllint --encode utf8 --format curl.tmp.atom
34 entries=$(xmllint --xpath 'count(/*/*[local-name()="entry"])' curl.tmp.atom)
35 assert_equal 1 "${entries}" 35 "Atom feed expected 1 = ${entries}"
37 echo "####################################################"
38 echo "## Step 1: fetch token to login and add a new link: "
39 echo "GET ${BASE_URL}?post=..."
40 rm curl.tmp.*
41 # http://unix.stackexchange.com/a/157219
42 LOCATION=$(curl --get --url "${BASE_URL}" \
43 --data-urlencode "post=https://github.com/sebsauvage/Shaarli/commit/450342737ced8ef2864b4f83a4107a7fafcc4add" \
44 --data-urlencode "title=Initial Commit to Shaarli on Github." \
45 --data-urlencode "source=Source Text" \
46 --cookie curl.cook --cookie-jar curl.cook \
47 --location --output curl.tmp.html \
48 --trace-ascii curl.tmp.trace --dump-header curl.tmp.head \
49 --write-out '%{url_effective}' 2>/dev/null)
50 # todo:
51 errmsg=$(xmllint --html --nowarning --xpath 'string(/html[1 = count(*)]/head[1 = count(*)]/script[starts-with(.,"alert(")])' curl.tmp.html)
52 assert_equal "" "${errmsg}" 52 "error: '${errmsg}'"
53 TOKEN=$(xmllint --html --nowarning --xpath 'string(/html/body//form[@name="loginform"]//input[@name="token"]/@value)' curl.tmp.html)
54 # string(..) http://stackoverflow.com/a/18390404
56 # the precise length doesn't matter, it just has to be significantly larger than ''
57 assert_equal 40 $(printf "%s" ${TOKEN} | wc -c) 57 "expected TOKEN of 40 characters, but found ${TOKEN} of $(printf "%s" ${TOKEN} | wc -c)"
59 echo "######################################################"
60 echo "## Step 2: follow the redirect, do the login and get the post form: "
61 echo "POST ${LOCATION}"
62 rm curl.tmp.*
63 LOCATION=$(curl --url "${LOCATION}" \
64 --data-urlencode "login=${USERNAME}" \
65 --data-urlencode "password=${PASSWORD}" \
66 --data-urlencode "token=${TOKEN}" \
67 --cookie curl.cook --cookie-jar curl.cook \
68 --location --output curl.tmp.html \
69 --trace-ascii curl.tmp.trace --dump-header curl.tmp.head \
70 --write-out '%{url_effective}' 2>/dev/null)
71 # todo:
72 errmsg=$(xmllint --html --nowarning --xpath 'string(/html[1 = count(*)]/head[1 = count(*)]/script[starts-with(.,"alert(")])' curl.tmp.html)
73 assert_equal "" "${errmsg}" 73 "error: '${errmsg}'"
74 # check presence of various mandatory form fields:
75 for field in lf_url lf_title lf_linkdate lf_tags token
77 assert_equal 1 $(xmllint --html --nowarning --xpath "count(/html/body//form[@name = 'linkform']//input[@name='${field}'])" curl.tmp.html) 77 "expected to have a '${field}'"
78 done
79 for field in lf_description
81 assert_equal 1 $(xmllint --html --nowarning --xpath "count(/html/body//form[@name = 'linkform']//textarea[@name='${field}'])" curl.tmp.html) 81 "expected to have a '${field}'"
82 done
84 # turn form field data into curl post data file
85 xmllint --html --nowarning --xmlout curl.tmp.html | xmllint --xpath '/html/body//form[@name="linkform"]' - | /usr/bin/env ruby ../scripts/form2post.rb > curl.post
87 echo "######################################################"
88 echo "## Step 3: finally post the link: "
89 echo "POST ${LOCATION}"
90 rm curl.tmp.*
91 LOCATION=$(curl --url "${LOCATION}" \
92 --data "@curl.post" \
93 --data-urlencode "lf_linkdate=20130226_100941" \
94 --data-urlencode "lf_source=$0" \
95 --data-urlencode "lf_description=Must be older because http://sebsauvage.github.io/Shaarli/ mentions 'Copyright (c) 2011 Sébastien SAUVAGE (sebsauvage.net)'." \
96 --data-urlencode "lf_tags=t1 t2" \
97 --data-urlencode "save_edit=Save" \
98 --cookie curl.cook --cookie-jar curl.cook \
99 --output curl.tmp.html \
100 --trace-ascii curl.tmp.trace --dump-header curl.tmp.head \
101 --write-out '%{redirect_url}' 2>/dev/null)
102 # don't use --location and url_effective because this strips /?#... on curl 7.30.0 (x86_64-apple-darwin13.0)
103 echo "final ${LOCATION}"
104 # todo:
105 errmsg=$(xmllint --html --nowarning --xpath 'string(/html[1 = count(*)]/head[1 = count(*)]/script[starts-with(.,"alert(")])' curl.tmp.html 2>/dev/null)
106 assert_equal "" "${errmsg}" 106 "error: '${errmsg}'"
107 echo "${LOCATION}" | egrep -e "^${BASE_URL}/\?#[a-zA-Z0-9@_-]{6}\$" || assert_fail 108 "expected link hash url, but got '${LOCATION}'"
108 # don't follow the redirect => no html => no logout link [ 1 -eq "$(xmllint --html --nowarning --xpath "count(/html/body//a[@href = '?do=logout'])" curl.tmp.html 2>/dev/null)" ] || assert_fail 13 "I expected a logout link."
110 #####################################################
111 # TODO: watch out for error messages like e.g. ip bans or the like.
113 # check post-condition - there must be more entries now:
114 echo "###################################################"
115 echo "## Non-logged-in Atom feed after adding a link (should have the added + the initial public default entry):"
116 curl --silent --show-error --output curl.tmp.atom "${BASE_URL}/?do=atom"
117 xmllint --encode utf8 --format curl.tmp.atom
118 entries=$(xmllint --xpath 'count(/*/*[local-name()="entry"])' curl.tmp.atom)
119 assert_equal 2 "${entries}" 119 "Atom feed expected 2 = ${entries}"