Skip to content

Commit

Permalink
Merge pull request #9 from Dobefu/feature/api-routes
Browse files Browse the repository at this point in the history
Feature/api routes
  • Loading branch information
Dobefu authored Dec 22, 2024
2 parents 445e162 + 4b6e1d8 commit 05cc03a
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cmd/api/get-global-fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import (
"github.com/Dobefu/csb/cmd/cs_sdk"
)

func GetGlobalFields() (map[string]interface{}, error) {
data, err := cs_sdk.Request("global_fields", "GET", nil)

if err != nil {
return nil, err
}

return data, nil
}
28 changes: 28 additions & 0 deletions cmd/api/get-global-fields_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package api

import (
"os"
"testing"

"github.com/Dobefu/csb/cmd/init_env"
"github.com/stretchr/testify/assert"
)

func TestGetGlobalFields(t *testing.T) {
var err error

init_env.Main("../../.env.test")

oldApiKey := os.Getenv("CS_API_KEY")
os.Setenv("CS_API_KEY", "bogus")

_, err = GetGlobalFields()
assert.NotEqual(t, nil, err)

os.Setenv("CS_API_KEY", oldApiKey)

globalFields, err := GetGlobalFields()
assert.Equal(t, nil, err)

assert.NotEqual(t, nil, globalFields)
}
1 change: 1 addition & 0 deletions cmd/server/handle-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func HandleRoutes(mux *http.ServeMux, apiPath string) {
apiRoute(mux, apiPath, "/get-entry-by-uid", "GET", v1.GetEntryByUid)
apiRoute(mux, apiPath, "/content-types", "GET", v1.GetContentTypes)
apiRoute(mux, apiPath, "/content-type", "GET", v1.GetContentType)
apiRoute(mux, apiPath, "/global-fields", "GET", v1.GetGlobalFields)
}

func apiRoute(
Expand Down
93 changes: 91 additions & 2 deletions cmd/server/handle-routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

func TestHandleRoutes(t *testing.T) {
init_env.Main("../../.env.test")

var err error

init_env.Main("../../.env.test")

err = database.Connect()
assert.Equal(t, nil, err)

Expand Down Expand Up @@ -83,6 +83,20 @@ func TestHandleRoutes(t *testing.T) {
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

oldApiKey := os.Getenv("CS_API_KEY")
os.Setenv("CS_API_KEY", "bogus")

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/get-entry-by-url?url=/&locale=en"),
true,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

os.Setenv("CS_API_KEY", oldApiKey)

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/get-entry-by-uid"),
Expand All @@ -101,6 +115,20 @@ func TestHandleRoutes(t *testing.T) {
assert.NotEqual(t, nil, body["data"])
assert.Equal(t, nil, body["error"])

oldApiKey = os.Getenv("CS_API_KEY")
os.Setenv("CS_API_KEY", "bogus")

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/get-entry-by-uid?uid=blt0617c28651fb44bf&locale=en"),
true,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

os.Setenv("CS_API_KEY", oldApiKey)

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/get-entry-by-uid?uid=/bogus&locale=en"),
Expand Down Expand Up @@ -139,6 +167,22 @@ func TestHandleRoutes(t *testing.T) {
assert.NotEqual(t, nil, body["data"])
assert.Equal(t, nil, body["error"])

os.Setenv("DEBUG_AUTH_BYPASS", "")

oldApiKey = os.Getenv("CS_API_KEY")
os.Setenv("CS_API_KEY", "bogus")

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/content-types"),
true,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

os.Setenv("CS_API_KEY", oldApiKey)

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/content-type"),
Expand All @@ -165,6 +209,51 @@ func TestHandleRoutes(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/global-fields"),
true,
)
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, body["data"])
assert.Equal(t, nil, body["error"])

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/global-fields"),
false,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

os.Setenv("DEBUG_AUTH_BYPASS", "1")

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/global-fields"),
false,
)
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, body["data"])
assert.Equal(t, nil, body["error"])

os.Setenv("DEBUG_AUTH_BYPASS", "")

oldApiKey = os.Getenv("CS_API_KEY")
os.Setenv("CS_API_KEY", "bogus")

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/global-fields"),
true,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])

os.Setenv("CS_API_KEY", oldApiKey)
}

func request(method string, path string, withAuthToken bool) (body map[string]interface{}, err error) {
Expand Down
100 changes: 100 additions & 0 deletions cmd/server/middleware/require-delivery-token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package middleware

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/Dobefu/csb/cmd/init_env"
"github.com/stretchr/testify/assert"
)

func TestRequireDeliveryToken(t *testing.T) {
var err error

init_env.Main("../../../.env.test")

mux := http.NewServeMux()
RequireDeliveryToken(mux)

server := httptest.NewServer(mux)
assert.NotEqual(t, nil, server)
defer server.Close()

mux.Handle(
"/",
RequireDeliveryToken(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "")
})),
)

err = request(
fmt.Sprintf("%s/%s", server.URL, "/"),
false,
)
assert.Equal(t, nil, err)

err = request(
fmt.Sprintf("%s/%s", server.URL, "/"),
true,
)
assert.Equal(t, nil, err)

os.Setenv("DEBUG_AUTH_BYPASS", "1")

err = request(
fmt.Sprintf("%s/%s", server.URL, "/"),
false,
)
assert.Equal(t, nil, err)

os.Setenv("DEBUG_AUTH_BYPASS", "")

oldAuthToken := os.Getenv("CS_DELIVERY_TOKEN")
os.Setenv("CS_DELIVERY_TOKEN", "bogus")

err = request(
fmt.Sprintf("%s/%s", server.URL, "/"),
false,
)
assert.Equal(t, nil, err)

os.Setenv("CS_DELIVERY_TOKEN", "")

err = request(
fmt.Sprintf("%s/%s", server.URL, "/"),
false,
)
assert.Equal(t, nil, err)

os.Setenv("CS_DELIVERY_TOKEN", oldAuthToken)
}

func request(path string, withAuthToken bool) (err error) {
req, err := http.NewRequest(
"GET",
path,
nil,
)

if err != nil {
return err
}

if withAuthToken {
req.Header = http.Header{
"Authorization": {os.Getenv("CS_DELIVERY_TOKEN")},
}
}

client := http.Client{}
_, err = client.Do(req)

if err != nil {
return err
}

return nil
}
31 changes: 31 additions & 0 deletions cmd/server/routes/v1/get-global-fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package v1

import (
"encoding/json"
"fmt"
"net/http"

"github.com/Dobefu/csb/cmd/api"
"github.com/Dobefu/csb/cmd/server/utils"
)

func GetGlobalFields(w http.ResponseWriter, r *http.Request) {
globalFields, err := api.GetGlobalFields()

if err != nil {
utils.PrintError(w, err, false)
return
}

output := utils.ConstructOutput()
output["data"] = globalFields

json, err := json.Marshal(output)

if err != nil {
utils.PrintError(w, err, true)
return
}

fmt.Fprint(w, string(json))
}

0 comments on commit 05cc03a

Please sign in to comment.