summaryrefslogtreecommitdiff
path: root/src/cli/test_anastasis_reducer_select_country.sh
blob: c02f61f23d364915ba6ec4ba871f70a319375d16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# This file is in the public domain.

set -eu

# Exit, with status code "skip" (no 'real' failure)
function exit_skip() {
    echo " SKIP: $1"
    exit 77
}

# Exit, with error message (hard failure)
function exit_fail() {
    echo " FAIL: $1"
    exit 1
}

# Cleanup to run whenever we exit
function cleanup()
{
    for n in `jobs -p`
    do
        kill $n 2> /dev/null || true
    done
    rm -f $TFILE
    wait
}



TFILE=`mktemp test_reducer_stateXXXXXX`

# Install cleanup handler (except for kill -9)
trap cleanup EXIT

# Check we can actually run
echo -n "Testing for jq"
jq -h > /dev/null || exit_skip "jq required"
echo " FOUND"

echo -n "Testing for anastasis-reducer ..."
anastasis-reducer -h > /dev/null || exit_skip "anastasis-reducer required"
echo " FOUND"



# Test continent re-selection
echo -n "Test continent re-selection ..."
anastasis-reducer -a '{"continent": "Europe"}' select_continent resources/01-recovery.json $TFILE

echo -n "."


STATE=`jq -r -e .recovery_state < $TFILE`
if test "$STATE" != "COUNTRY_SELECTING"
then
    exit_fail "Expected new state to be COUNTRY_SELECTING, got $STATE"
fi

echo -n "."

jq -e .countries[0] < $TFILE > /dev/null || exit_fail "Expected new state to include countries"
jq -e .countries[0].code < $TFILE > /dev/null || exit_fail "Expected new state to include countries with code"
jq -e .countries[0].continent < $TFILE > /dev/null || exit_fail "Expected new state to include countries with continent"
jq -e .countries[0].name < $TFILE > /dev/null || exit_fail "Expected new state to include countries with name"
jq -e .countries[0].currency < $TFILE > /dev/null || exit_fail "Expected new state to include countries with currency"

SELECTED_CONTINENT=`jq -r -e .selected_continent < $TFILE`
if test "$SELECTED_CONTINENT" != "Europe"
then
    exit_fail "Expected selected continent to be 'Testcontinent', got $SELECTED_CONTINENT"
fi

echo " OK"


echo -n "Test invalid continent re-selection ..."
anastasis-reducer -a '{"continent": "Pangaia"}' select_continent resources/00-recovery.json $TFILE 2> /dev/null \
        && exit_fail "Expected selection to fail. Check '$TFILE'"

echo " OK"


echo -n "Test NX country selection ..."

anastasis-reducer -a \
  '{"country_code": "zz",
    "currencies": ["EUR" ]}' \
        select_country \
        resources/01-backup.json $TFILE 2> /dev/null \
        && exit_fail "Expected selection to fail. Check '$TFILE'"

echo " OK"

echo -n "Test invalid country selection for continent ..."

anastasis-reducer -a \
  '{"country_code": "de",
    "currencies":["EUR"]}' \
        select_country \
        resources/01-backup.json $TFILE 2> /dev/null \
        && exit_fail "Expected selection to fail. Check '$TFILE'"

echo " OK"

echo -n "Test country selection ..."

anastasis-reducer -a \
  '{"country_code": "xx",
    "currencies":["TESTKUDOS"]}' \
  select_country resources/01-backup.json $TFILE

STATE=`jq -r -e .backup_state < $TFILE`
if test "$STATE" != "USER_ATTRIBUTES_COLLECTING"
then
    exit_fail "Expected new state to be 'USER_ATTRIBUTES_COLLECTING', got '$STATE'"
fi
echo -n "."
SELECTED_COUNTRY=`jq -r -e .selected_country < $TFILE`
if test "$SELECTED_COUNTRY" != "xx"
then
    exit_fail "Expected selected country to be 'xx', got '$SELECTED_COUNTRY'"
fi
echo -n "."
SELECTED_CURRENCY=`jq -r -e .currencies[0] < $TFILE`
if test "$SELECTED_CURRENCY" != "TESTKUDOS"
then
    exit_fail "Expected selected currency to be 'TESTKUDOS', got '$SELECTED_CURRENCY'"
fi
echo -n "."
REQ_ATTRIBUTES=`jq -r -e .required_attributes < $TFILE`
if test "$REQ_ATTRIBUTES" == NULL
then
    exit_fail "Expected required attributes array not to be NULL"
fi
echo -n "."
AUTH_PROVIDERS=`jq -r -e .authentication_providers < $TFILE`
if test "$AUTH_PROVIDERS" == NULL
then
    exit_fail "Expected authentication_providers array not to be NULL"
fi

echo " OK"

exit 0