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
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 xsltproc
--version 1>/dev
/null || assert_fail
102 "I need xsltproc (libxml2)."
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 [ -r "$0".xslt
] || assert_fail
4 "How strange, helper xslt script '$0.xslt' not readable."
30 echo "####################################################"
31 echo "## Step 1: fetch token to login "
32 echo "GET ${BASE_URL}?do=login"
34 # http://unix.stackexchange.com/a/157219
35 LOCATION
=$
(curl
--get --url "${BASE_URL}/?do=login" \
36 --cookie curl.cook
--cookie-jar curl.cook \
37 --location --output curl.tmp.html \
38 --trace-ascii curl.tmp.trace
--dump-header curl.tmp.
head \
39 --write-out '%{url_effective}' 2>/dev
/null
)
41 errmsg
=$
(xmllint
--html --nowarning --xpath 'string(/html[1 = count(*)]/head[1 = count(*)]/script[starts-with(.,"alert(")])' curl.tmp.html
)
42 assert_equal
"" "${errmsg}" 42 "fetch token error"
43 TOKEN
=$
(xmllint
--html --nowarning --xpath 'string(/html/body//form[@name="loginform"]//input[@name="token"]/@value)' curl.tmp.html
)
44 # string(..) http://stackoverflow.com/a/18390404
46 # the precise length doesn't matter, it just has to be significantly larger than ''
47 assert_equal
40 $
(printf "%s" ${TOKEN} | wc -c) 47 "expected TOKEN of 40 characters, but found ${TOKEN} of $(printf "%s" ${TOKEN} |
wc -c)"
49 echo "######################################################"
50 echo "## Step 2: follow the redirect, do the login and redirect to ${BASE_URL}/? "
51 echo "POST ${LOCATION}"
53 LOCATION
=$
(curl
--url "${LOCATION}" \
54 --data-urlencode "login=${USERNAME}" \
55 --data-urlencode "password=${PASSWORD}" \
56 --data-urlencode "token=${TOKEN}" \
57 --data-urlencode "returnurl=${BASE_URL}/?" \
58 --cookie curl.cook
--cookie-jar curl.cook \
59 --location --output curl.tmp.html \
60 --trace-ascii curl.tmp.trace
--dump-header curl.tmp.
head \
61 --write-out '%{url_effective}' 2>/dev
/null
)
62 errmsg
=$
(xmllint
--html --nowarning --xpath 'string(/html[1 = count(*)]/head[1 = count(*)]/script[starts-with(.,"alert(")])' curl.tmp.html
)
63 assert_equal
"" "${errmsg}" 64 "do login error"
64 assert_equal
"${BASE_URL}/?" "${LOCATION}" 65 "redirect to BASE_URL"
67 echo "###################################################"
68 echo "## Logged-in Atom feed prior doing anything (should have 2 entries)"
69 curl
--url "${BASE_URL}/?do=atom" \
70 --silent --show-error \
71 --cookie curl.cook
--cookie-jar curl.cook \
72 --output curl.tmp.atom
73 entries
=$
(xmllint
--xpath 'count(/*/*[local-name()="entry"])' curl.tmp.atom
)
74 assert_equal
"2" "${entries}" 74 "Atom feed entries"
76 # now figure out the precise lf_linkdate and token for each entry to delete
79 # re-extract the token from the most recent HTTP response as it's consumed after each
80 # HTTP request. So a simple for loop doesn't do the trick.
82 line
="$(xsltproc --html --nonet "${0}".xslt curl.tmp.html 2>/dev/null | head -n 1)"
83 [ "" = "$line" ] && break
85 echo "${line}" |
while read lf_linkdate token
87 echo "lf_linkdate=${lf_linkdate} token=${token}"
88 http_code
=$
(curl
--url "${BASE_URL}/" \
89 --data-urlencode "lf_linkdate=${lf_linkdate}" \
90 --data-urlencode "token=${token}" \
91 --data-urlencode "delete_link=" \
92 --cookie curl.cook
--cookie-jar curl.cook \
93 --location --output curl.tmp.html \
94 --trace-ascii curl.tmp.trace
--dump-header curl.tmp.
head \
95 --write-out '%{http_code}' 2>/dev
/null
)
96 assert_equal
"200" "${http_code}" 92 "POST lf_linkdate=${lf_linkdate}&token=..."
97 break # process only one line at a time.
102 # check post-condition
103 echo "###################################################"
104 echo "## Logged-in Atom feed after deleting all entries"
105 curl
--url "${BASE_URL}/?do=atom" \
106 --silent --show-error \
107 --cookie curl.cook
--cookie-jar curl.cook \
108 --output curl.tmp.atom
109 entries
=$
(xmllint
--xpath 'count(/*/*[local-name()="entry"])' curl.tmp.atom
)
110 assert_equal
"0" "${entries}" 103 "Atom feed entries"