github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/gui_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api_test
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/json"
     9  	"io/ioutil"
    10  	"net/http"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	"github.com/juju/version"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/apiserver/params"
    17  )
    18  
    19  // sendJSONResponse encodes the given content as JSON and writes it to the
    20  // given response writer.
    21  func sendJSONResponse(c *gc.C, w http.ResponseWriter, content interface{}) {
    22  	w.Header().Set("Content-Type", params.ContentTypeJSON)
    23  	encoder := json.NewEncoder(w)
    24  	err := encoder.Encode(content)
    25  	c.Assert(err, jc.ErrorIsNil)
    26  }
    27  
    28  func (s *clientSuite) TestGUIArchives(c *gc.C) {
    29  	client := s.APIState.Client()
    30  	called := false
    31  	response := params.GUIArchiveResponse{
    32  		Versions: []params.GUIArchiveVersion{{
    33  			Version: version.MustParse("1.0.0"),
    34  			SHA256:  "hash1",
    35  			Current: false,
    36  		}, {
    37  			Version: version.MustParse("2.0.0"),
    38  			SHA256:  "hash2",
    39  			Current: true,
    40  		}},
    41  	}
    42  
    43  	// Set up a fake endpoint for tests.
    44  	defer fakeAPIEndpoint(c, client, "/gui-archive", "GET",
    45  		func(w http.ResponseWriter, req *http.Request) {
    46  			defer req.Body.Close()
    47  			called = true
    48  			sendJSONResponse(c, w, response)
    49  		},
    50  	).Close()
    51  
    52  	versions, err := client.GUIArchives()
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	c.Assert(versions, jc.DeepEquals, response.Versions)
    55  	c.Assert(called, jc.IsTrue)
    56  }
    57  
    58  func (s *clientSuite) TestGUIArchivesError(c *gc.C) {
    59  	client := s.APIState.Client()
    60  
    61  	// Set up a fake endpoint for tests.
    62  	defer fakeAPIEndpoint(c, client, "/gui-archive", "GET",
    63  		func(w http.ResponseWriter, req *http.Request) {
    64  			defer req.Body.Close()
    65  			w.WriteHeader(http.StatusBadRequest)
    66  		},
    67  	).Close()
    68  
    69  	versions, err := client.GUIArchives()
    70  	c.Assert(err, gc.ErrorMatches, "cannot retrieve GUI archives info: .*")
    71  	c.Assert(versions, gc.IsNil)
    72  }
    73  
    74  func (s *clientSuite) TestUploadGUIArchive(c *gc.C) {
    75  	client := s.APIState.Client()
    76  	called := false
    77  	archive := []byte("archive content")
    78  	hash, size, vers := "archive-hash", int64(len(archive)), version.MustParse("2.1.0")
    79  
    80  	// Set up a fake endpoint for tests.
    81  	defer fakeAPIEndpoint(c, client, "/gui-archive", "POST",
    82  		func(w http.ResponseWriter, req *http.Request) {
    83  			defer req.Body.Close()
    84  			called = true
    85  			err := req.ParseForm()
    86  			c.Assert(err, jc.ErrorIsNil)
    87  			// Check version and content length.
    88  			c.Assert(req.Form.Get("version"), gc.Equals, vers.String())
    89  			c.Assert(req.ContentLength, gc.Equals, size)
    90  			// Check request body.
    91  			obtainedArchive, err := ioutil.ReadAll(req.Body)
    92  			c.Assert(err, jc.ErrorIsNil)
    93  			c.Assert(obtainedArchive, gc.DeepEquals, archive)
    94  			// Check hash.
    95  			h := req.Form.Get("hash")
    96  			c.Assert(h, gc.Equals, hash)
    97  			// Send the response.
    98  			sendJSONResponse(c, w, params.GUIArchiveVersion{
    99  				Current: true,
   100  			})
   101  		},
   102  	).Close()
   103  
   104  	current, err := client.UploadGUIArchive(bytes.NewReader(archive), hash, size, vers)
   105  	c.Assert(err, jc.ErrorIsNil)
   106  	c.Assert(current, jc.IsTrue)
   107  	c.Assert(called, jc.IsTrue)
   108  }
   109  
   110  func (s *clientSuite) TestUploadGUIArchiveError(c *gc.C) {
   111  	client := s.APIState.Client()
   112  	archive := []byte("archive content")
   113  	hash, size, vers := "archive-hash", int64(len(archive)), version.MustParse("2.1.0")
   114  
   115  	// Set up a fake endpoint for tests.
   116  	defer fakeAPIEndpoint(c, client, "/gui-archive", "POST",
   117  		func(w http.ResponseWriter, req *http.Request) {
   118  			defer req.Body.Close()
   119  			w.WriteHeader(http.StatusBadRequest)
   120  		},
   121  	).Close()
   122  
   123  	current, err := client.UploadGUIArchive(bytes.NewReader(archive), hash, size, vers)
   124  	c.Assert(err, gc.ErrorMatches, "cannot upload the GUI archive: .*")
   125  	c.Assert(current, jc.IsFalse)
   126  }
   127  
   128  func (s *clientSuite) TestSelectGUIVersion(c *gc.C) {
   129  	client := s.APIState.Client()
   130  	called := false
   131  	vers := version.MustParse("2.0.42")
   132  
   133  	// Set up a fake endpoint for tests.
   134  	defer fakeAPIEndpoint(c, client, "/gui-version", "PUT",
   135  		func(w http.ResponseWriter, req *http.Request) {
   136  			defer req.Body.Close()
   137  			called = true
   138  			// Check request body.
   139  			var request params.GUIVersionRequest
   140  			decoder := json.NewDecoder(req.Body)
   141  			err := decoder.Decode(&request)
   142  			c.Assert(err, jc.ErrorIsNil)
   143  			c.Assert(request.Version, gc.Equals, vers)
   144  		},
   145  	).Close()
   146  
   147  	err := client.SelectGUIVersion(vers)
   148  	c.Assert(err, jc.ErrorIsNil)
   149  	c.Assert(called, jc.IsTrue)
   150  }
   151  
   152  func (s *clientSuite) TestSelectGUIVersionError(c *gc.C) {
   153  	client := s.APIState.Client()
   154  	vers := version.MustParse("2.0.42")
   155  
   156  	// Set up a fake endpoint for tests.
   157  	defer fakeAPIEndpoint(c, client, "/gui-version", "PUT",
   158  		func(w http.ResponseWriter, req *http.Request) {
   159  			defer req.Body.Close()
   160  			w.WriteHeader(http.StatusBadRequest)
   161  		},
   162  	).Close()
   163  
   164  	err := client.SelectGUIVersion(vers)
   165  	c.Assert(err, gc.ErrorMatches, "cannot select GUI version: .*")
   166  }