github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/service/rest_project_test.go (about)

     1  package service
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/codegangsta/negroni"
    11  	"github.com/evergreen-ci/evergreen"
    12  	"github.com/evergreen-ci/evergreen/db"
    13  	"github.com/evergreen-ci/evergreen/model"
    14  	serviceutil "github.com/evergreen-ci/evergreen/service/testutil"
    15  	"github.com/evergreen-ci/evergreen/testutil"
    16  	"github.com/evergreen-ci/render"
    17  	. "github.com/smartystreets/goconvey/convey"
    18  )
    19  
    20  var projectTestConfig = testutil.TestConfig()
    21  
    22  func init() {
    23  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(projectTestConfig))
    24  }
    25  
    26  func TestProjectRoutes(t *testing.T) {
    27  	uis := UIServer{
    28  		RootURL:     projectTestConfig.Ui.Url,
    29  		Settings:    *projectTestConfig,
    30  		UserManager: serviceutil.MockUserManager{},
    31  	}
    32  	home := evergreen.FindEvergreenHome()
    33  	uis.Render = render.New(render.Options{
    34  		Directory:    filepath.Join(home, WebRootPath, Templates),
    35  		DisableCache: true,
    36  	})
    37  	testutil.HandleTestingErr(uis.InitPlugins(), t, "error installing plugins")
    38  	router, err := uis.NewRouter()
    39  	testutil.HandleTestingErr(err, t, "error setting up router")
    40  	n := negroni.New()
    41  	n.Use(negroni.HandlerFunc(UserMiddleware(uis.UserManager)))
    42  	n.UseHandler(router)
    43  
    44  	Convey("When loading a public project, it should be found", t, func() {
    45  		testutil.HandleTestingErr(db.Clear(model.ProjectRefCollection), t,
    46  			"Error clearing '%v' collection", model.ProjectRefCollection)
    47  
    48  		publicId := "pub"
    49  		public := &model.ProjectRef{
    50  			Identifier:  publicId,
    51  			Enabled:     true,
    52  			Repo:        "repo1",
    53  			LocalConfig: "buildvariants:\n - name: ubuntu",
    54  			Admins:      []string{},
    55  		}
    56  		So(public.Insert(), ShouldBeNil)
    57  
    58  		url, err := router.Get("project_info").URL("project_id", publicId)
    59  		So(err, ShouldBeNil)
    60  		request, err := http.NewRequest("GET", url.String(), nil)
    61  		So(err, ShouldBeNil)
    62  
    63  		response := httptest.NewRecorder()
    64  
    65  		Convey("by a public user", func() {
    66  			n.ServeHTTP(response, request)
    67  			outRef := &model.ProjectRef{}
    68  			So(response.Code, ShouldEqual, http.StatusOK)
    69  			So(json.Unmarshal(response.Body.Bytes(), outRef), ShouldBeNil)
    70  			So(outRef, ShouldResemble, public)
    71  		})
    72  		Convey("and a logged-in user", func() {
    73  			request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
    74  			n.ServeHTTP(response, request)
    75  			outRef := &model.ProjectRef{}
    76  			So(response.Code, ShouldEqual, http.StatusOK)
    77  			So(json.Unmarshal(response.Body.Bytes(), outRef), ShouldBeNil)
    78  			So(outRef, ShouldResemble, public)
    79  		})
    80  		Convey("and be visible to the project_list route", func() {
    81  			url, err := router.Get("project_list").URL()
    82  			So(err, ShouldBeNil)
    83  			request, err := http.NewRequest("GET", url.String(), nil)
    84  			So(err, ShouldBeNil)
    85  			n.ServeHTTP(response, request)
    86  			out := struct {
    87  				Projects []string `json:"projects"`
    88  			}{}
    89  			So(response.Code, ShouldEqual, http.StatusOK)
    90  			So(json.Unmarshal(response.Body.Bytes(), &out), ShouldBeNil)
    91  			So(len(out.Projects), ShouldEqual, 1)
    92  			So(out.Projects[0], ShouldEqual, public.Identifier)
    93  		})
    94  	})
    95  
    96  	Convey("When loading a private project", t, func() {
    97  		testutil.HandleTestingErr(db.Clear(model.ProjectRefCollection), t,
    98  			"Error clearing '%v' collection", model.ProjectRefCollection)
    99  
   100  		privateId := "priv"
   101  		private := &model.ProjectRef{
   102  			Identifier: privateId,
   103  			Enabled:    true,
   104  			Private:    true,
   105  			Repo:       "repo1",
   106  			Admins:     []string{},
   107  		}
   108  		So(private.Insert(), ShouldBeNil)
   109  		response := httptest.NewRecorder()
   110  
   111  		Convey("users who are not logged in should be denied with a 401", func() {
   112  			url, err := router.Get("project_info").URL("project_id", privateId)
   113  			So(err, ShouldBeNil)
   114  			request, err := http.NewRequest("GET", url.String(), nil)
   115  			So(err, ShouldBeNil)
   116  			n.ServeHTTP(response, request)
   117  
   118  			So(response.Code, ShouldEqual, http.StatusUnauthorized)
   119  		})
   120  
   121  		Convey("users who are logged in should be able to access the project", func() {
   122  			url, err := router.Get("project_info").URL("project_id", privateId)
   123  			So(err, ShouldBeNil)
   124  			request, err := http.NewRequest("GET", url.String(), nil)
   125  			So(err, ShouldBeNil)
   126  			// add auth cookie--this can be anything if we are using a MockUserManager
   127  			request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
   128  			n.ServeHTTP(response, request)
   129  
   130  			outRef := &model.ProjectRef{}
   131  			So(response.Code, ShouldEqual, http.StatusOK)
   132  			So(json.Unmarshal(response.Body.Bytes(), outRef), ShouldBeNil)
   133  			So(outRef, ShouldResemble, private)
   134  		})
   135  		Convey("and it should be visible to the project_list route", func() {
   136  			url, err := router.Get("project_list").URL()
   137  			So(err, ShouldBeNil)
   138  			request, err := http.NewRequest("GET", url.String(), nil)
   139  			So(err, ShouldBeNil)
   140  			Convey("for credentialed users", func() {
   141  				request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
   142  				n.ServeHTTP(response, request)
   143  				out := struct {
   144  					Projects []string `json:"projects"`
   145  				}{}
   146  				So(response.Code, ShouldEqual, http.StatusOK)
   147  				So(json.Unmarshal(response.Body.Bytes(), &out), ShouldBeNil)
   148  				So(len(out.Projects), ShouldEqual, 1)
   149  				So(out.Projects[0], ShouldEqual, private.Identifier)
   150  			})
   151  			Convey("but not public users", func() {
   152  				n.ServeHTTP(response, request)
   153  				out := struct {
   154  					Projects []string `json:"projects"`
   155  				}{}
   156  				So(response.Code, ShouldEqual, http.StatusOK)
   157  				So(json.Unmarshal(response.Body.Bytes(), &out), ShouldBeNil)
   158  				So(len(out.Projects), ShouldEqual, 0)
   159  			})
   160  		})
   161  	})
   162  
   163  	Convey("When finding info on a nonexistent project", t, func() {
   164  		url, err := router.Get("project_info").URL("project_id", "nope")
   165  		So(err, ShouldBeNil)
   166  
   167  		request, err := http.NewRequest("GET", url.String(), nil)
   168  		So(err, ShouldBeNil)
   169  		response := httptest.NewRecorder()
   170  
   171  		Convey("response should contain a sensible error message", func() {
   172  			Convey("for a public user", func() {
   173  				n.ServeHTTP(response, request)
   174  				So(response.Code, ShouldEqual, http.StatusNotFound)
   175  				var jsonBody map[string]interface{}
   176  				err = json.Unmarshal(response.Body.Bytes(), &jsonBody)
   177  				So(err, ShouldBeNil)
   178  				So(len(jsonBody["message"].(string)), ShouldBeGreaterThan, 0)
   179  			})
   180  			Convey("and a logged-in user", func() {
   181  				request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
   182  				n.ServeHTTP(response, request)
   183  				So(response.Code, ShouldEqual, http.StatusNotFound)
   184  				var jsonBody map[string]interface{}
   185  				err = json.Unmarshal(response.Body.Bytes(), &jsonBody)
   186  				So(err, ShouldBeNil)
   187  				So(len(jsonBody["message"].(string)), ShouldBeGreaterThan, 0)
   188  			})
   189  		})
   190  	})
   191  }