Skip to content

Commit

Permalink
Throw error on POST /tyk/apis/ if UseDBAppConfigs is enabled
Browse files Browse the repository at this point in the history
Refer to TykTechnologies#177

Calling POST to /tyk/apis/ when `UseDBAppConfigs` is enabled may
cause an unexpected behavior. It added a new API definition to
`config.AppPath` directory and didn't add to MongoDB, so it's not
available on the dashboard.

This commit changes the behavior of POST command by rejecting
request if `UseDBAppConfigs` setting is enabled and suggest user to
use an Advanced Management API instead.
  • Loading branch information
iwat committed Jan 23, 2016
1 parent 755fa55 commit 42a1d58
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ func HandleGetAPI(APIID string) ([]byte, int) {
}

func HandleAddOrUpdateApi(APIID string, r *http.Request) ([]byte, int) {
if config.UseDBAppConfigs {
log.Error("Rejected new API Definition due to UseDBAppConfigs = true")
return createError("Due to enabled use_db_app_configs, please use Advanced Management API"), 500
}

success := true
decoder := json.NewDecoder(r.Body)
var responseMessage []byte
Expand Down
63 changes: 62 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -86,7 +87,8 @@ type testAPIDefinition struct {
func init() {
// Clean up our API list
log.Debug("Setting up Empty API path")
config.AppPath = "./test/"
config.AppPath = os.TempDir() + "/tyk_test/"
os.Mkdir(config.AppPath, 0755)
}

func TestHealthCheckEndpoint(t *testing.T) {
Expand Down Expand Up @@ -188,6 +190,65 @@ func TestApiHandlerGetSingle(t *testing.T) {
}
}

func TestApiHandlerPost(t *testing.T) {
log.Debug("TEST POST SINGLE API DEFINITION")
uri := "/tyk/apis/1"
method := "POST"

recorder := httptest.NewRecorder()
param := make(url.Values)

req, err := http.NewRequest(method, uri+param.Encode(), strings.NewReader(apiTestDef))

if err != nil {
t.Fatal(err)
}

apiHandler(recorder, req)

var success Success
err = json.Unmarshal([]byte(recorder.Body.String()), &success)

if err != nil {
t.Error("Could not unmarshal POST result:\n", err, recorder.Body.String())
} else {
if success.Status != "ok" {
t.Error("Response is incorrect - not success :\n", recorder.Body.String())
}
}
}

func TestApiHandlerPostDbConfig(t *testing.T) {
log.Debug("TEST POST SINGLE API DEFINITION ON USE_DB_CONFIG")
uri := "/tyk/apis/1"
method := "POST"

config.UseDBAppConfigs = true
defer func() { config.UseDBAppConfigs = false }()

recorder := httptest.NewRecorder()
param := make(url.Values)

req, err := http.NewRequest(method, uri+param.Encode(), strings.NewReader(apiTestDef))

if err != nil {
t.Fatal(err)
}

apiHandler(recorder, req)

var success Success
err = json.Unmarshal([]byte(recorder.Body.String()), &success)

if err != nil {
t.Error("Could not unmarshal POST result:\n", err, recorder.Body.String())
} else {
if success.Status == "ok" {
t.Error("Response is incorrect - expected error due to use_db_app_config :\n", recorder.Body.String())
}
}
}

func TestKeyHandlerNewKey(t *testing.T) {
uri := "/tyk/keys/1234"
method := "POST"
Expand Down

0 comments on commit 42a1d58

Please sign in to comment.