Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit-Tests]: sendEdgeListToJarvis #2389

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions routes/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package routes

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stakwork/sphinx-tribes/config"
"github.com/stakwork/sphinx-tribes/utils"
"github.com/stretchr/testify/assert"
)

func TestSendEdgeListToJarvis(t *testing.T) {
originalURL := config.JarvisUrl
originalToken := config.JarvisToken
defer func() {
config.JarvisUrl = originalURL
config.JarvisToken = originalToken
}()

tests := []struct {
name string
edgeList utils.EdgeList
setupMock func() *httptest.Server
setupConfig func()
expectedError bool
}{
{
name: "Basic Functionality",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
Source: utils.Node{
NodeType: "test",
NodeData: map[string]interface{}{"test": "data"},
},
Targets: []utils.Node{
{
NodeType: "test",
NodeData: map[string]interface{}{"test": "data"},
},
},
},
},
},
setupMock: func() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
assert.Equal(t, "test-token", r.Header.Get("x-api-token"))
w.WriteHeader(http.StatusOK)
}))
},
setupConfig: func() {
config.JarvisToken = "test-token"
},
expectedError: false,
},
{
name: "Empty EdgeList",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{},
},
setupMock: func() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
},
setupConfig: func() {
config.JarvisToken = "test-token"
},
expectedError: false,
},
{
name: "Missing JarvisUrl",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
},
},
},
setupMock: nil,
setupConfig: func() {
config.JarvisUrl = ""
config.JarvisToken = "test-token"
},
expectedError: false,
},
{
name: "Missing JarvisToken",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
},
},
},
setupMock: nil,
setupConfig: func() {
config.JarvisUrl = "http://test.com"
config.JarvisToken = ""
},
expectedError: false,
},
{
name: "Invalid JSON Marshalling",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
Source: utils.Node{
NodeType: "test",
NodeData: make(chan int),
},
},
},
},
setupMock: func() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
},
setupConfig: func() {
config.JarvisToken = "test-token"
},
expectedError: false,
},
{
name: "HTTP Request Creation Failure",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
},
},
},
setupMock: nil,
setupConfig: func() {
config.JarvisUrl = "http://[::1]:namedport"
config.JarvisToken = "test-token"
},
expectedError: false,
},
{
name: "Network Failure",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
},
},
},
setupMock: func() *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("Force connection close")
}))
server.Close()
return server
},
setupConfig: func() {
config.JarvisToken = "test-token"
},
expectedError: true,
},
{
name: "Non-2xx HTTP Response",
edgeList: utils.EdgeList{
EdgeList: []utils.Edge{
{
Edge: utils.EdgeInfo{
EdgeType: "test",
Weight: 1.0,
},
},
},
},
setupMock: func() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{
"error": "Internal Server Error",
})
}))
},
setupConfig: func() {
config.JarvisToken = "test-token"
},
expectedError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setupConfig != nil {
tt.setupConfig()
}

if tt.setupMock != nil {
server := tt.setupMock()
defer server.Close()
config.JarvisUrl = server.URL
}

err := sendEdgeListToJarvis(tt.edgeList)

if tt.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
Loading