Skip to content

Commit

Permalink
Cleanup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tcr-ableton committed Aug 22, 2019
1 parent 15d0c70 commit 6b55bb5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 82 deletions.
2 changes: 1 addition & 1 deletion cmd/trousseau/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func PullCommand() cli.Command {
trousseau.ErrorLogger.Fatal(err)
}

trousseau.InfoLogger.Println("Encrypted data store succesfully pulled from remote storage\n")
trousseau.InfoLogger.Println("Encrypted data store succesfully pulled from remote storage")

return nil
},
Expand Down
50 changes: 26 additions & 24 deletions internal/store/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@ package store

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestKVStoreGet(t *testing.T) {
kvStore := make(KVStore)
kvStore["abc"] = 123

v, err := kvStore.Get("abc")
ok(t, err)
equals(t, v, 123)
assert.Nil(t, err)
assert.Equal(t, v, 123)
}

func TestKVStoreGet_errors_on_non_existing_key(t *testing.T) {
kvStore := make(KVStore)

v, err := kvStore.Get("easy as")
notOk(t, err)
equals(t, v, "")
assert.NotNil(t, err)
assert.Equal(t, v, "")
}

func TestKVStoreSet(t *testing.T) {
kvStore := make(KVStore)

kvStore.Set("abc", 123)
equals(t, kvStore["abc"], 123)
assert.Equal(t, kvStore["abc"], 123)
}

func TestKVStoreDel(t *testing.T) {
Expand All @@ -34,7 +36,7 @@ func TestKVStoreDel(t *testing.T) {

kvStore.Del("abc")
_, in := kvStore["abc"]
assert(t, in == false, "Expected 'abc' key to be removed from KVStore Container")
assert.True(t, in == false, "Expected 'abc' key to be removed from KVStore Container")
}

func TestKVStoreRename_without_overwrite_and_non_existing_destination_key(t *testing.T) {
Expand All @@ -45,10 +47,10 @@ func TestKVStoreRename_without_overwrite_and_non_existing_destination_key(t *tes
_, srcIn := kvStore["abc"]
_, destIn := kvStore["easy as"]

ok(t, err)
assert(t, srcIn == false, "Expected source key to have been removed from KVStore")
assert(t, destIn == true, "Expected destination key to be present in KVStore")
equals(t, kvStore["easy as"], 123)
assert.Nil(t, err)
assert.True(t, srcIn == false, "Expected source key to have been removed from KVStore")
assert.True(t, destIn == true, "Expected destination key to be present in KVStore")
assert.Equal(t, kvStore["easy as"], 123)
}

func TestKVStoreRename_without_overwrite_and_existing_destination_key(t *testing.T) {
Expand All @@ -60,10 +62,10 @@ func TestKVStoreRename_without_overwrite_and_existing_destination_key(t *testing
_, srcIn := kvStore["abc"]
_, destIn := kvStore["easy as"]

notOk(t, err)
assert(t, srcIn == true, "Expected source key to have been removed from KVStore")
assert(t, destIn == true, "Expected destination key to be present in KVStore")
equals(t, kvStore["easy as"], "do re mi")
assert.NotNil(t, err)
assert.True(t, srcIn == true, "Expected source key to have been removed from KVStore")
assert.True(t, destIn == true, "Expected destination key to be present in KVStore")
assert.Equal(t, kvStore["easy as"], "do re mi")
}

func TestKVStoreRename_with_overwrite_and_existing_destination_key(t *testing.T) {
Expand All @@ -75,10 +77,10 @@ func TestKVStoreRename_with_overwrite_and_existing_destination_key(t *testing.T)
_, srcIn := kvStore["abc"]
_, destIn := kvStore["easy as"]

ok(t, err)
assert(t, srcIn == false, "Expected source key to have been removed from KVStore")
assert(t, destIn == true, "Expected destination key to be present in KVStore")
equals(t, kvStore["easy as"], 123)
assert.Nil(t, err)
assert.True(t, srcIn == false, "Expected source key to have been removed from KVStore")
assert.True(t, destIn == true, "Expected destination key to be present in KVStore")
assert.Equal(t, kvStore["easy as"], 123)
}

func TestKVStoreRename_with_overwrite_and_non_existing_destination_key(t *testing.T) {
Expand All @@ -89,10 +91,10 @@ func TestKVStoreRename_with_overwrite_and_non_existing_destination_key(t *testin
_, srcIn := kvStore["abc"]
_, destIn := kvStore["easy as"]

ok(t, err)
assert(t, srcIn == false, "Expected source key to have been removed from KVStore")
assert(t, destIn == true, "Expected destination key to be present in KVStore")
equals(t, kvStore["easy as"], 123)
assert.Nil(t, err)
assert.True(t, srcIn == false, "Expected source key to have been removed from KVStore")
assert.True(t, destIn == true, "Expected destination key to be present in KVStore")
assert.Equal(t, kvStore["easy as"], 123)
}

func TestKVStoreKeys(t *testing.T) {
Expand All @@ -101,7 +103,7 @@ func TestKVStoreKeys(t *testing.T) {
kvStore["easy as"] = "do re mi"

keys := kvStore.Keys()
equals(t, keys, []string{"abc", "easy as"})
assert.Equal(t, keys, []string{"abc", "easy as"})
}

func TestKVStoreItems(t *testing.T) {
Expand All @@ -114,5 +116,5 @@ func TestKVStoreItems(t *testing.T) {
"abc": 123,
"easy as": "do re mi",
}
equals(t, items, expected)
assert.Equal(t, items, expected)
}
44 changes: 0 additions & 44 deletions internal/trousseau/test_helpers.go

This file was deleted.

10 changes: 6 additions & 4 deletions internal/trousseau/trousseau_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"testing"

"github.com/oleiade/tempura"
"os"

"github.com/oleiade/tempura"
"github.com/stretchr/testify/assert"
)

func TestOpenTrousseau(t *testing.T) {
Expand All @@ -24,9 +26,9 @@ func TestOpenTrousseau(t *testing.T) {
t.Fatal(err)
}

assert(t, tr.CryptoType == ASYMMETRIC_ENCRYPTION, "Wrong encryption type")
assert(t, tr.CryptoAlgorithm == GPG_ENCRYPTION, "Wrong encryption algorithm")
equals(t, tr.Data, []byte("abc"))
assert.True(t, tr.CryptoType == ASYMMETRIC_ENCRYPTION, "Wrong encryption type")
assert.True(t, tr.CryptoAlgorithm == GPG_ENCRYPTION, "Wrong encryption algorithm")
assert.Equal(t, tr.Data, []byte("abc"))
}

func TestOpenTrousseau_returns_err_when_file_does_not_exist(t *testing.T) {
Expand Down
15 changes: 8 additions & 7 deletions internal/trousseau/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import (
"testing"

"github.com/oleiade/serrure/openpgp"
"github.com/stretchr/testify/assert"
)

func TestIsVersionZeroDotThree(t *testing.T) {
data := []byte(openpgp.PGP_MESSAGE_HEADER +
"12kjd091jd192jd0192jd" +
openpgp.PGP_MESSAGE_FOOTER)

assert(t,
assert.True(t,
isVersionZeroDotThreeDotZero(data) == true,
"Input test data were suppose to match version 0.3.N")
}

func TestIsVersionZeroDotThree_fails_with_data_shorter_than_pgp_header(t *testing.T) {
data := []byte("abc123")
assert(t,
assert.True(t,
isVersionZeroDotThreeDotZero(data) == false,
"Input test data weren't suppose to match version 0.3.N")
}
Expand All @@ -36,7 +37,7 @@ func TestIsVersionZeroDotFour(t *testing.T) {
t.Error(err)
}

assert(t,
assert.True(t,
isVersionZeroDotThreeDotOne(data) == true,
"Input test data were suppose to match version 0.4.N")
}
Expand All @@ -49,7 +50,7 @@ func TestDiscoverVersion_with_only_one_valid_version_in_mapping(t *testing.T) {
"0.3.0": isVersionZeroDotThreeDotZero,
}

assert(t,
assert.True(t,
DiscoverVersion(data, mapping) == "0.3.0",
"Version 0.3.0 was supposed to be discovered")
}
Expand All @@ -70,7 +71,7 @@ func TestDiscoverVersion_with_two_valid_versions_in_mapping(t *testing.T) {
t.Error(err)
}

assert(t,
assert.True(t,
DiscoverVersion(data, mapping) == "0.4.0",
"Version 0.4.0 was supposed to be discovered")
}
Expand All @@ -84,7 +85,7 @@ func TestDiscoverVersion_with_two_matching_version_returns_the_lowest(t *testing
"0.3.1": isVersionZeroDotThreeDotZero,
}

assert(t,
assert.True(t,
DiscoverVersion(data, mapping) == "0.3.0",
"Version 0.3.0 was supposed to be discovered")

Expand All @@ -97,5 +98,5 @@ func TestDiscoverVersion_with_no_matching_version(t *testing.T) {
"0.4.0": isVersionZeroDotThreeDotOne,
}

equals(t, DiscoverVersion(data, mapping), "")
assert.Equal(t, DiscoverVersion(data, mapping), "")
}
4 changes: 2 additions & 2 deletions pkg/gpgagent/gpgagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPrompt(t *testing.T) {
Desc: "Type 'foo' for testing",
Error: "seriously, or I'll be an error.",
Prompt: "foo",
CacheKey: fmt.Sprintf("gpgagent_test-cachekey-%d", time.Now()),
CacheKey: fmt.Sprintf("gpgagent_test-cachekey-%d", time.Now().Unix()),
}
s1, err := conn.GetPassphrase(req)
if err != nil {
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestPrompt(t *testing.T) {
Desc: "Press Cancel for testing",
Error: "seriously, or I'll be an error.",
Prompt: "cancel!",
CacheKey: fmt.Sprintf("gpgagent_test-cachekey-%d", time.Now()),
CacheKey: fmt.Sprintf("gpgagent_test-cachekey-%d", time.Now().Unix()),
})
if err != ErrCancel {
t.Errorf("expected cancel, got %q, %v", s4, err)
Expand Down

0 comments on commit 6b55bb5

Please sign in to comment.