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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package gui_test
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"net/url"
    10  	"strings"
    11  
    12  	"github.com/juju/httprequest"
    13  	jc "github.com/juju/testing/checkers"
    14  	"github.com/juju/webbrowser"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/cmd/juju/gui"
    18  	jujutesting "github.com/juju/juju/juju/testing"
    19  	coretesting "github.com/juju/juju/testing"
    20  )
    21  
    22  type guiSuite struct {
    23  	jujutesting.JujuConnSuite
    24  }
    25  
    26  var _ = gc.Suite(&guiSuite{})
    27  
    28  // run executes the gui command passing the given args.
    29  func (s *guiSuite) run(c *gc.C, args ...string) (string, error) {
    30  	ctx, err := coretesting.RunCommand(c, gui.NewGUICommand(), args...)
    31  	return strings.Trim(coretesting.Stderr(ctx), "\n"), err
    32  }
    33  
    34  func (s *guiSuite) patchClient(f func(*httprequest.Client, string) error) {
    35  	if f == nil {
    36  		f = func(*httprequest.Client, string) error {
    37  			return nil
    38  		}
    39  	}
    40  	s.PatchValue(gui.ClientGet, f)
    41  }
    42  
    43  func (s *guiSuite) patchBrowser(f func(*url.URL) error) {
    44  	if f == nil {
    45  		f = func(*url.URL) error {
    46  			return nil
    47  		}
    48  	}
    49  	s.PatchValue(gui.WebbrowserOpen, f)
    50  }
    51  
    52  func (s *guiSuite) guiURL(c *gc.C) string {
    53  	info := s.APIInfo(c)
    54  	return fmt.Sprintf("https://%s/gui/%s/", info.Addrs[0], info.ModelTag.Id())
    55  }
    56  
    57  func (s *guiSuite) TestGUISuccessWithBrowser(c *gc.C) {
    58  	var clientURL, browserURL string
    59  	s.patchClient(func(client *httprequest.Client, u string) error {
    60  		clientURL = u
    61  		return nil
    62  	})
    63  	s.patchBrowser(func(u *url.URL) error {
    64  		browserURL = u.String()
    65  		return nil
    66  	})
    67  	out, err := s.run(c)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	guiURL := s.guiURL(c)
    70  	expectOut := "Opening the Juju GUI in your browser.\nIf it does not open, open this URL:\n" + guiURL
    71  	c.Assert(out, gc.Equals, expectOut)
    72  	c.Assert(clientURL, gc.Equals, guiURL)
    73  	c.Assert(browserURL, gc.Equals, guiURL)
    74  }
    75  
    76  func (s *guiSuite) TestGUISuccessWithCredentials(c *gc.C) {
    77  	s.patchClient(nil)
    78  	s.patchBrowser(nil)
    79  	out, err := s.run(c, "--show-credentials")
    80  	c.Assert(err, jc.ErrorIsNil)
    81  	c.Assert(out, jc.Contains, "Username: admin@local\nPassword: dummy-secret")
    82  }
    83  
    84  func (s *guiSuite) TestGUISuccessNoBrowser(c *gc.C) {
    85  	s.patchClient(nil)
    86  	// There is no need to patch the browser open function here.
    87  	out, err := s.run(c, "--no-browser")
    88  	c.Assert(err, jc.ErrorIsNil)
    89  	c.Assert(out, gc.Equals, s.guiURL(c))
    90  }
    91  
    92  func (s *guiSuite) TestGUISuccessBrowserNotFound(c *gc.C) {
    93  	s.patchClient(nil)
    94  	s.patchBrowser(func(u *url.URL) error {
    95  		return webbrowser.ErrNoBrowser
    96  	})
    97  	out, err := s.run(c)
    98  	c.Assert(err, jc.ErrorIsNil)
    99  	expectOut := "Open this URL in your browser:\n" + s.guiURL(c)
   100  	c.Assert(out, gc.Equals, expectOut)
   101  }
   102  
   103  func (s *guiSuite) TestGUIErrorBrowser(c *gc.C) {
   104  	s.patchClient(nil)
   105  	s.patchBrowser(func(u *url.URL) error {
   106  		return errors.New("bad wolf")
   107  	})
   108  	out, err := s.run(c)
   109  	c.Assert(err, gc.ErrorMatches, "cannot open web browser: bad wolf")
   110  	c.Assert(out, gc.Equals, "")
   111  }
   112  
   113  func (s *guiSuite) TestGUIErrorUnavailable(c *gc.C) {
   114  	s.patchClient(func(client *httprequest.Client, u string) error {
   115  		return errors.New("bad wolf")
   116  	})
   117  	out, err := s.run(c)
   118  	c.Assert(err, gc.ErrorMatches, "Juju GUI is not available: bad wolf")
   119  	c.Assert(out, gc.Equals, "")
   120  }