Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #85 from troyronda/linttest
Browse files Browse the repository at this point in the history
ci: include test files in linter
  • Loading branch information
George Aristy authored Aug 12, 2019
2 parents 6be39fe + a066d4b commit a8eff94
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 42 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ run:
tests: true
build-tags: [""]
skip-dirs: [""]
skip-files:
- ^(.*_test.go.*$)$

output:
format: colored-line-number
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ func TestLogLevel(t *testing.T) {
//TestParseLevelError testing 'LogLevel()' used for parsing log levels from strings
func TestParseLevelError(t *testing.T) {

verifyLevelError := func(expected Level, levels ...string) {
verifyLevelError := func(levels ...string) {
for _, level := range levels {
_, err := ParseLevel(level)
require.Error(t, err, "not supposed to succeed while parsing level string [%s]", level)
}
}

verifyLevelError(DEBUG, "", "D", "DE BUG", ".")
verifyLevelError("", "D", "DE BUG", ".")

}

Expand Down
2 changes: 2 additions & 0 deletions pkg/didcomm/protocol/exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestGenerateInviteWithPublicDID(t *testing.T) {
DID: "did:example:ZadolSRQkehfo",
})

require.NoError(t, err)
require.NotEmpty(t, invite)

invite, err = GenerateInviteWithPublicDID(&Invitation{
Expand All @@ -50,6 +51,7 @@ func TestGenerateInviteWithKeyAndEndpoint(t *testing.T) {
ServiceEndpoint: "https://example.com/endpoint",
RoutingKeys: []string{"8HH5gYEeNc3z7PYXmd54d4x6qAfCNrqQqEB3nS7Zfu7K"},
})
require.NoError(t, err)
require.NotEmpty(t, invite)

invite, err = GenerateInviteWithKeyAndEndpoint(&Invitation{
Expand Down
20 changes: 14 additions & 6 deletions pkg/didcomm/transport/http/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestInboundHandler(t *testing.T) {
require.NotNil(t, inHandler)
server := startMockServer(inHandler)
port := getServerPort(server)
serverUrl := fmt.Sprintf("https://localhost:%d", port)
serverURL := fmt.Sprintf("https://localhost:%d", port)
defer func() {
e := server.Close()
if e != nil {
Expand Down Expand Up @@ -64,25 +64,33 @@ func TestInboundHandler(t *testing.T) {
}

// test http.Get should should fail (not supported)
rs, err := client.Get(serverUrl + "/")
rs, err := client.Get(serverURL + "/")
require.NoError(t, err)
err = rs.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusMethodNotAllowed, rs.StatusCode)

// test accepted HTTP method (POST) but with bad content type
rs, err = client.Post(serverUrl+"/", "bad-content-type", bytes.NewBuffer([]byte("Hello World")))
rs, err = client.Post(serverURL+"/", "bad-content-type", bytes.NewBuffer([]byte("Hello World")))
require.NoError(t, err)
err = rs.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusUnsupportedMediaType, rs.StatusCode)

// test with nil body ..
rs, err = client.Post(serverUrl+"/", commContentType, nil)
rs, err = client.Post(serverURL+"/", commContentType, nil)
require.NoError(t, err)
err = rs.Body.Close()
require.NoError(t, err)
require.Equal(t, http.StatusBadRequest, rs.StatusCode)

// finally test successful POST requests
data := "success"

resp, e := client.Post(serverUrl+"/", commContentType, bytes.NewBuffer([]byte(data)))
require.NoError(t, e)
resp, err := client.Post(serverURL+"/", commContentType, bytes.NewBuffer([]byte(data)))
require.NoError(t, err)
err = resp.Body.Close()
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, http.StatusAccepted, resp.StatusCode)
}
12 changes: 6 additions & 6 deletions pkg/didcomm/transport/http/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func TestWithOutboundOpts(t *testing.T) {

func TestOutboundHTTPTransport(t *testing.T) {
// prepare http server
server := startMockServer(mockHttpHandler{})
server := startMockServer(mockHTTPHandler{})

port := getServerPort(server)
serverUrl := fmt.Sprintf("https://localhost:%d", port)
serverURL := fmt.Sprintf("https://localhost:%d", port)
defer func() {
err := server.Close()
if err != nil {
Expand All @@ -57,12 +57,12 @@ func TestOutboundHTTPTransport(t *testing.T) {
Certificates: nil,
}
// create a new invalid Outbound transport instance
ot, err := NewOutbound()
_, err = NewOutbound()
require.Error(t, err)
require.EqualError(t, err, "Can't create an outbound transport without an HTTP client")

// now create a new valid Outbound transport instance and test its Send() call
ot, err = NewOutbound(WithOutboundTLSConfig(tlsConfig), WithOutboundTimeout(clientTimeout))
ot, err := NewOutbound(WithOutboundTLSConfig(tlsConfig), WithOutboundTimeout(clientTimeout))
require.NoError(t, err)
require.NotNil(t, ot)

Expand All @@ -78,12 +78,12 @@ func TestOutboundHTTPTransport(t *testing.T) {
require.Empty(t, r)

// and try with a 'bad' payload with a valid url..
r, e = ot.Send("bad", serverUrl)
r, e = ot.Send("bad", serverURL)
require.Error(t, e)
require.Empty(t, r)

// finally using a valid url
r, e = ot.Send("Hello World", serverUrl)
r, e = ot.Send("Hello World", serverURL)
require.NoError(t, e)
require.NotEmpty(t, r)

Expand Down
8 changes: 4 additions & 4 deletions pkg/didcomm/transport/http/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func addCertsToCertPool(pool *x509.CertPool) error {

func startMockServer(handler http.Handler) net.Listener {
// ":0" will make the listener auto assign a free port
listener, err := net.Listen("tcp", ":0")
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
logger.Fatalf("HTTP listener failed to start: %s", err)
}
Expand All @@ -59,10 +59,10 @@ func startMockServer(handler http.Handler) net.Listener {
return listener
}

type mockHttpHandler struct {
type mockHTTPHandler struct {
}

func (m mockHttpHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
func (m mockHTTPHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req.Body != nil {
body, err := ioutil.ReadAll(req.Body)
if err != nil || string(body) == "bad" {
Expand All @@ -74,7 +74,7 @@ func (m mockHttpHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {

// mocking successful response
res.WriteHeader(http.StatusAccepted) // usually DID-Comm expects StatusAccepted code (202)
res.Write([]byte("success"))
res.Write([]byte("success")) // nolint
}

// decodeCerts will decode a list of pemCertsList (string) into a list of x509 certificates
Expand Down
20 changes: 9 additions & 11 deletions pkg/didmethod/peer/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/stretchr/testify/require"
)

const peerDID = "did:peer:1234"

const peerDIDDoc = `{
"@context": ["https://w3id.org/did/v1","https://w3id.org/did/v2"],
"id": "did:peer:1234",
Expand Down Expand Up @@ -72,28 +74,26 @@ func TestPeerDIDResolver(t *testing.T) {
dbstore, err := prov.GetStoreHandle()
require.NoError(t, err)

did1 := "did:peer:1234"

// save did document
store := NewDIDStore(dbstore)
err = store.Put(did1, &did.Doc{ID: did1}, nil)
err = store.Put(peerDID, &did.Doc{ID: peerDID}, nil)
require.NoError(t, err)

resl := NewDIDResolver(store)
doc, err := resl.Read(did1, nil, "", false)
doc, err := resl.Read(peerDID, nil, "", false)
require.NoError(t, err)

document := &did.Doc{}
err = json.Unmarshal(doc, document)
require.NoError(t, err)
require.Equal(t, did1, document.ID)
require.Equal(t, peerDID, document.ID)

// empty DID
doc, err = resl.Read("", nil, "", false)
_, err = resl.Read("", nil, "", false)
require.Error(t, err)

// missing DID
doc, err = resl.Read("did:peer:789", nil, "", false)
_, err = resl.Read("did:peer:789", nil, "", false)
require.Error(t, err)

}
Expand All @@ -107,18 +107,16 @@ func TestWithDIDResolveAPI(t *testing.T) {
dbstore, err := prov.GetStoreHandle()
require.NoError(t, err)

did1 := "did:peer:1234"

// save did document
store := NewDIDStore(dbstore)
peerDoc, err := diddoc.FromBytes([]byte(peerDIDDoc))
require.NoError(t, err)
require.NotNil(t, peerDoc)
err = store.Put(did1, peerDoc, nil)
err = store.Put(peerDID, peerDoc, nil)
require.NoError(t, err)

r := didresolver.New(didresolver.WithDidMethod("peer", NewDIDResolver(store)))
_, err = r.Resolve("did:peer:1234")
_, err = r.Resolve(peerDID)
require.NoError(t, err)

_, err = r.Resolve("did:peer:789")
Expand Down
9 changes: 6 additions & 3 deletions pkg/didmethod/peer/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func setupLevelDB(t testing.TB) (string, func()) {
t.Fatalf("Failed to create leveldb directory: %s", err)
}
return dbPath, func() {
os.RemoveAll(dbPath)
err := os.RemoveAll(dbPath)
if err != nil {
t.Fatalf("Failed to clear leveldb directory: %s", err)
}
}
}

Expand Down Expand Up @@ -54,11 +57,11 @@ func TestPeerDIDStore(t *testing.T) {
require.Equal(t, did1, doc.ID)

// get - empty id
doc, err = store.Get("")
_, err = store.Get("")
require.Error(t, err)

// get - invalid id
doc, err = store.Get("did:peer:789")
_, err = store.Get("did:peer:789")
require.Error(t, err)

// put - empty id
Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/common/logging/metadata/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func TestParseLevel(t *testing.T) {

func TestParseLevelError(t *testing.T) {

verifyLevelError := func(expected Level, levels ...string) {
verifyLevelError := func(levels ...string) {
for _, level := range levels {
_, err := ParseLevel(level)
require.Error(t, err, "not supposed to succeed while parsing level string [%s]", level)
}
}

verifyLevelError(DEBUG, "", "D", "DE BUG", ".")
verifyLevelError("", "D", "DE BUG", ".")

}

Expand Down
23 changes: 17 additions & 6 deletions pkg/storage/leveldb/leveldb_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func setupLevelDB(t testing.TB) (string, func()) {
t.Fatalf("Failed to create leveldb directory: %s", err)
}
return dbPath, func() {
os.RemoveAll(dbPath)
err := os.RemoveAll(dbPath)
if err != nil {
t.Fatalf("Failed to clear leveldb directory: %s", err)
}
}
}

Expand All @@ -32,19 +35,20 @@ func TestLevelDBStore(t *testing.T) {
store, _ := prov.GetStoreHandle()

did1 := "did:example:123"
store.Put(did1, []byte("value"))
err := store.Put(did1, []byte("value"))
require.NoError(t, err)

doc, err := store.Get(did1)
require.NoError(t, err)
require.NotEmpty(t, doc)
require.Equal(t, []byte("value"), doc)

did2 := "did:example:789"
doc, err = store.Get(did2)
_, err = store.Get(did2)
require.Error(t, err)

// nil key
doc, err = store.Get("")
_, err = store.Get("")
require.Error(t, err)

// nil value
Expand All @@ -59,15 +63,22 @@ func TestLevelDBStore(t *testing.T) {
require.NoError(t, err)

// try to get after provider is closed
doc, err = store.Get(did1)
_, err = store.Get(did1)
require.Error(t, err)

// pass file instead of directory for leveldb
file, err := ioutil.TempFile("", "leveldb.txt")
if err != nil {
t.Fatalf("Failed to create leveldb file: %s", err)
}
defer os.Remove(file.Name())
defer cleanupFile(t, file)
_, err = NewProvider(file.Name())
require.Error(t, err)
}

func cleanupFile(t *testing.T, file *os.File) {
err := os.Remove(file.Name())
if err != nil {
t.Fatalf("Failed to cleanup file: %s", file.Name())
}
}

0 comments on commit a8eff94

Please sign in to comment.