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

🧪 test: tests for the REST API #294

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
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
🧪 test: tests for the REST API
  • Loading branch information
shurco committed Oct 8, 2024
commit 22d84a50051c1a6fc833e7b37b1c1044d721601b
124 changes: 124 additions & 0 deletions api/agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package agent

import (
"net/http"
"testing"

"github.com/werbot/werbot/internal/trace"
"github.com/werbot/werbot/internal/utils/test"
)

func TestHandler_auth(t *testing.T) {
app, teardownTestCase, _, _ := setupTest(t)
defer teardownTestCase(t)

testTable := []test.APITable{
{
Name: "test0_01",
Method: http.MethodPost,
Path: test.PathGluing(pathAgent, "auth"),
StatusCode: 404,
Body: test.BodyNotFound,
},
{
Name: "test0_02",
Method: http.MethodPost,
Path: test.PathGluing(pathAgent, "auth", test.ConstFakeID),
StatusCode: 404,
Body: test.BodyTable{
"code": float64(404),
"message": "Not Found",
"result": trace.MsgTokenNotFound,
},
},
{
Name: "test0_03",
Method: http.MethodPost,
Path: test.PathGluing(pathAgent, "auth", "0a177fc3-ad38-40c6-b936-ded649ce5a57"),
StatusCode: 200,
Body: test.BodyTable{
"code": float64(200),
"message": "Auth data",
"result.api_key": "5tYJOkr3oLCOEvhw3nB83AmDzYM7yJsJ0Sonl",
"result.api_secret": "aDzYMy9g3mmsq3XazPLvvCbj4kJAsgatxBDVW",
"result.scheme_type": float64(103),
},
},
}

test.RunCaseAPITests(t, app, testTable)
}

func TestHandler_addScheme(t *testing.T) {
app, teardownTestCase, adminHeader, _ := setupTest(t)
defer teardownTestCase(t)

testTable := []test.APITable{
{
Name: "test0_01",
Method: http.MethodPost,
Path: test.PathGluing(pathAgentScheme),
StatusCode: 401,
Body: test.BodyUnauthorized,
},
{ // ADMIN:
Name: "test1_01",
Method: http.MethodPost,
Path: test.PathGluing(pathAgentScheme),
StatusCode: 404,
Body: test.BodyNotFound,
RequestHeaders: adminHeader,
},
{ // ADMIN:
Name: "test1_02",
Method: http.MethodPost,
Path: test.PathGluing(pathAgentScheme, test.ConstFakeID),
StatusCode: 400,
Body: test.BodyTable{
"code": float64(400),
"message": "Bad Request",
"result.address": "value is required",
"result.port": "value is required",
"result.login": "value is required",
},
RequestHeaders: adminHeader,
},
{ // ADMIN:
Name: "test1_03",
Method: http.MethodPost,
Path: test.PathGluing(pathAgentScheme, test.ConstFakeID),
StatusCode: 404,
RequestBody: test.BodyTable{
"address": "127.0.0.255",
"port": float64(2922),
"login": "ubuntu",
},
Body: test.BodyTable{
"code": float64(404),
"message": "Not Found",
"result": trace.MsgTokenNotFound,
},
RequestHeaders: adminHeader,
},
{ // ADMIN:
Name: "test1_04",
Method: http.MethodPost,
Path: test.PathGluing(pathAgentScheme, "0a177fc3-ad38-40c6-b936-ded649ce5a57"),
StatusCode: 200,
RequestBody: test.BodyTable{
"address": "127.0.0.255",
"port": float64(2922),
"login": "ubuntu",
},
Body: test.BodyTable{
"code": float64(200),
"message": "Scheme data",
"result.public_key": "*",
"result.scheme_id": "*",
},
RequestHeaders: adminHeader,
},
}

test.RunCaseAPITests(t, app, testTable)
}
25 changes: 25 additions & 0 deletions api/agent/routes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package agent

import (
"testing"

"github.com/werbot/werbot/api/auth"
"github.com/werbot/werbot/internal/utils/test"
)

const (
pathAgent = "/v1/agent"
pathAgentScheme = "/v1/agent/scheme"
)

func setupTest(t *testing.T) (*test.APIHandler, func(t *testing.T), map[string]string, map[string]string) {
app, teardownTestCase := test.API(t)
auth.New(app.Handler).Routes()
New(app.Handler).Routes()
app.AddRoute404()

adminHeader := map[string]string{"x-api-key": test.ConstAdminProject1ApiKey}
userHeader := map[string]string{"x-api-key": test.ConstUserProject1ApiKey}

return app, teardownTestCase, adminHeader, userHeader
}
Loading
Loading