summaryrefslogtreecommitdiff
path: root/secrets/test/list.sh
blob: 08671c8fbb18681f6ef74315681f542f70c6e417 (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
#!/bin/bash
# Tests for list command

this_dir="$(cd "$(dirname "$0")" && pwd)"
source "${this_dir}/common.sh"

test_list_shows_secrets() {
    echo "value1" | szecret put "secret-a"
    echo "value2" | szecret put "secret-b"
    echo "value3" | szecret put "secret-c"

    local list_output
    list_output="$(szecret list)"

    assert_contains "${list_output}" "secret-a" "list output"
    assert_contains "${list_output}" "secret-b" "list output"
    assert_contains "${list_output}" "secret-c" "list output"
    pass "list shows all secrets"
}

test_list_empty_store() {
    local list_output
    list_output="$(szecret list)"
    assert_equals "" "${list_output}" "empty list"
    pass "list handles empty store"
}

test_list_sorted() {
    echo "v" | szecret put "zebra"
    echo "v" | szecret put "apple"
    echo "v" | szecret put "mango"

    local list_output
    list_output="$(szecret list)"

    # Check that apple comes before mango comes before zebra
    local apple_pos mango_pos zebra_pos
    apple_pos=$(echo "${list_output}" | grep -n "apple" | cut -d: -f1)
    mango_pos=$(echo "${list_output}" | grep -n "mango" | cut -d: -f1)
    zebra_pos=$(echo "${list_output}" | grep -n "zebra" | cut -d: -f1)

    if [[ "${apple_pos}" -lt "${mango_pos}" && "${mango_pos}" -lt "${zebra_pos}" ]]; then
        pass "list is sorted"
    else
        fail "list is not sorted: apple=${apple_pos}, mango=${mango_pos}, zebra=${zebra_pos}"
    fi
}

run_tests() {
    test_list_empty_store
    test_list_shows_secrets
    test_list_sorted
}

[[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests