github.com/iron-io/functions@v0.0.0-20180820112432-d59d7d1c40b2/api/server/routes_test.go (about)

     1  // +build server
     2  
     3  package server
     4  
     5  import (
     6  	"bytes"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/iron-io/functions/api/datastore"
    12  	"github.com/iron-io/functions/api/models"
    13  	"github.com/iron-io/functions/api/mqs"
    14  )
    15  
    16  func TestRouteCreate(t *testing.T) {
    17  	buf := setLogBuffer()
    18  	tasks := mockTasksConduit()
    19  	defer close(tasks)
    20  
    21  	for i, test := range []struct {
    22  		mock          models.Datastore
    23  		path          string
    24  		body          string
    25  		expectedCode  int
    26  		expectedError error
    27  	}{
    28  		// errors
    29  		{datastore.NewMock(), "/v1/apps/a/routes", ``, http.StatusBadRequest, models.ErrInvalidJSON},
    30  		{datastore.NewMock(), "/v1/apps/a/routes", `{ }`, http.StatusBadRequest, models.ErrRoutesMissingNew},
    31  		{datastore.NewMock(), "/v1/apps/a/routes", `{ "path": "/myroute" }`, http.StatusBadRequest, models.ErrRoutesMissingNew},
    32  		{datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingPath},
    33  		{datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { "path": "/myroute" } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingImage},
    34  		{datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { "image": "iron/hello" } }`, http.StatusBadRequest, models.ErrRoutesValidationMissingPath},
    35  		{datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { "image": "iron/hello", "path": "myroute" } }`, http.StatusBadRequest, models.ErrRoutesValidationInvalidPath},
    36  		{datastore.NewMock(), "/v1/apps/$/routes", `{ "route": { "image": "iron/hello", "path": "/myroute" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName},
    37  
    38  		// success
    39  		{datastore.NewMock(), "/v1/apps/a/routes", `{ "route": { "image": "iron/hello", "path": "/myroute" } }`, http.StatusOK, nil},
    40  	} {
    41  		rnr, cancel := testRunner(t)
    42  		srv := testServer(test.mock, &mqs.Mock{}, rnr, tasks)
    43  
    44  		body := bytes.NewBuffer([]byte(test.body))
    45  		_, rec := routerRequest(t, srv.Router, "POST", test.path, body)
    46  
    47  		if rec.Code != test.expectedCode {
    48  			t.Log(buf.String())
    49  			t.Errorf("Test %d: Expected status code to be %d but was %d",
    50  				i, test.expectedCode, rec.Code)
    51  		}
    52  
    53  		if test.expectedError != nil {
    54  			resp := getErrorResponse(t, rec)
    55  			if resp.Error == nil {
    56  				t.Log(buf.String())
    57  				t.Errorf("Test %d: Expected error message to have `%s`, but it was nil",
    58  					i, test.expectedError)
    59  			} else if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
    60  				t.Log(buf.String())
    61  				t.Errorf("Test %d: Expected error message to have `%s`, but it was `%s`",
    62  					i, test.expectedError, resp.Error.Message)
    63  			}
    64  		}
    65  		cancel()
    66  	}
    67  }
    68  
    69  func TestRouteDelete(t *testing.T) {
    70  	buf := setLogBuffer()
    71  	tasks := mockTasksConduit()
    72  	defer close(tasks)
    73  
    74  	for i, test := range []struct {
    75  		ds            models.Datastore
    76  		path          string
    77  		body          string
    78  		expectedCode  int
    79  		expectedError error
    80  	}{
    81  		{datastore.NewMock(), "/v1/apps/a/routes/missing", "", http.StatusNotFound, nil},
    82  		{datastore.NewMockInit(nil,
    83  			[]*models.Route{
    84  				{Path: "/myroute", AppName: "a"},
    85  			},
    86  		), "/v1/apps/a/routes/myroute", "", http.StatusOK, nil},
    87  	} {
    88  		rnr, cancel := testRunner(t)
    89  		srv := testServer(test.ds, &mqs.Mock{}, rnr, tasks)
    90  		_, rec := routerRequest(t, srv.Router, "DELETE", test.path, nil)
    91  
    92  		if rec.Code != test.expectedCode {
    93  			t.Log(buf.String())
    94  			t.Errorf("Test %d: Expected status code to be %d but was %d",
    95  				i, test.expectedCode, rec.Code)
    96  		}
    97  
    98  		if test.expectedError != nil {
    99  			resp := getErrorResponse(t, rec)
   100  
   101  			if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
   102  				t.Log(buf.String())
   103  				t.Errorf("Test %d: Expected error message to have `%s`",
   104  					i, test.expectedError.Error())
   105  			}
   106  		}
   107  		cancel()
   108  	}
   109  }
   110  
   111  func TestRouteList(t *testing.T) {
   112  	buf := setLogBuffer()
   113  	tasks := mockTasksConduit()
   114  	defer close(tasks)
   115  
   116  	rnr, cancel := testRunner(t)
   117  	defer cancel()
   118  	srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks)
   119  
   120  	for i, test := range []struct {
   121  		path          string
   122  		body          string
   123  		expectedCode  int
   124  		expectedError error
   125  	}{
   126  		{"/v1/apps/a/routes", "", http.StatusOK, nil},
   127  	} {
   128  		_, rec := routerRequest(t, srv.Router, "GET", test.path, nil)
   129  
   130  		if rec.Code != test.expectedCode {
   131  			t.Log(buf.String())
   132  			t.Errorf("Test %d: Expected status code to be %d but was %d",
   133  				i, test.expectedCode, rec.Code)
   134  		}
   135  
   136  		if test.expectedError != nil {
   137  			resp := getErrorResponse(t, rec)
   138  
   139  			if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
   140  				t.Log(buf.String())
   141  				t.Errorf("Test %d: Expected error message to have `%s`",
   142  					i, test.expectedError.Error())
   143  			}
   144  		}
   145  	}
   146  }
   147  
   148  func TestRouteGet(t *testing.T) {
   149  	buf := setLogBuffer()
   150  	tasks := mockTasksConduit()
   151  	defer close(tasks)
   152  
   153  	rnr, cancel := testRunner(t)
   154  	defer cancel()
   155  
   156  	srv := testServer(datastore.NewMock(), &mqs.Mock{}, rnr, tasks)
   157  
   158  	for i, test := range []struct {
   159  		path          string
   160  		body          string
   161  		expectedCode  int
   162  		expectedError error
   163  	}{
   164  		{"/v1/apps/a/routes/myroute", "", http.StatusNotFound, nil},
   165  	} {
   166  		_, rec := routerRequest(t, srv.Router, "GET", test.path, nil)
   167  
   168  		if rec.Code != test.expectedCode {
   169  			t.Log(buf.String())
   170  			t.Errorf("Test %d: Expected status code to be %d but was %d",
   171  				i, test.expectedCode, rec.Code)
   172  		}
   173  
   174  		if test.expectedError != nil {
   175  			resp := getErrorResponse(t, rec)
   176  
   177  			if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
   178  				t.Log(buf.String())
   179  				t.Errorf("Test %d: Expected error message to have `%s`",
   180  					i, test.expectedError.Error())
   181  			}
   182  		}
   183  	}
   184  }
   185  
   186  func TestRouteUpdate(t *testing.T) {
   187  	buf := setLogBuffer()
   188  	tasks := mockTasksConduit()
   189  	defer close(tasks)
   190  
   191  	for i, test := range []struct {
   192  		ds            models.Datastore
   193  		path          string
   194  		body          string
   195  		expectedCode  int
   196  		expectedError error
   197  	}{
   198  		// errors
   199  		{datastore.NewMock(), "/v1/apps/a/routes/myroute/do", ``, http.StatusBadRequest, models.ErrInvalidJSON},
   200  		{datastore.NewMock(), "/v1/apps/a/routes/myroute/do", `{}`, http.StatusBadRequest, models.ErrRoutesMissingNew},
   201  		{datastore.NewMock(), "/v1/apps/a/routes/myroute/do", `{ "route": { "type": "invalid-type" } }`, http.StatusBadRequest, nil},
   202  		{datastore.NewMock(), "/v1/apps/a/routes/myroute/do", `{ "route": { "format": "invalid-format" } }`, http.StatusBadRequest, nil},
   203  
   204  		// success
   205  		{datastore.NewMockInit(nil,
   206  			[]*models.Route{
   207  				{
   208  					AppName: "a",
   209  					Path:    "/myroute/do",
   210  				},
   211  			},
   212  		), "/v1/apps/a/routes/myroute/do", `{ "route": { "image": "iron/hello" } }`, http.StatusOK, nil},
   213  
   214  		// Addresses #381
   215  		{datastore.NewMockInit(nil,
   216  			[]*models.Route{
   217  				{
   218  					AppName: "a",
   219  					Path:    "/myroute/do",
   220  				},
   221  			},
   222  		), "/v1/apps/a/routes/myroute/do", `{ "route": { "path": "/otherpath" } }`, http.StatusBadRequest, nil},
   223  	} {
   224  		rnr, cancel := testRunner(t)
   225  		srv := testServer(test.ds, &mqs.Mock{}, rnr, tasks)
   226  
   227  		body := bytes.NewBuffer([]byte(test.body))
   228  
   229  		_, rec := routerRequest(t, srv.Router, "PATCH", test.path, body)
   230  
   231  		if rec.Code != test.expectedCode {
   232  			t.Log(buf.String())
   233  			t.Errorf("Test %d: Expected status code to be %d but was %d: %s",
   234  				i, test.expectedCode, rec.Code, rec.Body.String())
   235  		}
   236  
   237  		if test.expectedError != nil {
   238  			resp := getErrorResponse(t, rec)
   239  
   240  			if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
   241  				t.Log(buf.String())
   242  				t.Errorf("Test %d: Expected error message to have `%s`",
   243  					i, test.expectedError.Error())
   244  			}
   245  		}
   246  		cancel()
   247  	}
   248  }