github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/user/common_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user_test
     5  
     6  import (
     7  	"path/filepath"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cmd/juju/user"
    13  	"github.com/juju/juju/environs/configstore"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type CommonSuite struct {
    18  	BaseSuite
    19  	serverFilename string
    20  }
    21  
    22  var _ = gc.Suite(&CommonSuite{})
    23  
    24  func (s *CommonSuite) SetUpTest(c *gc.C) {
    25  	s.BaseSuite.SetUpTest(c)
    26  	s.serverFilename = ""
    27  	s.PatchValue(user.ServerFileNotify, func(filename string) {
    28  		s.serverFilename = filename
    29  	})
    30  }
    31  
    32  // ConnectionEndpoint so this suite implements the EndpointProvider interface.
    33  func (s *CommonSuite) ConnectionEndpoint() (configstore.APIEndpoint, error) {
    34  	return configstore.APIEndpoint{
    35  		// NOTE: the content here is the same as t
    36  		Addresses: []string{"127.0.0.1:12345"},
    37  		CACert:    testing.CACert,
    38  	}, nil
    39  }
    40  
    41  func (s *CommonSuite) TestAbsolutePath(c *gc.C) {
    42  	ctx := testing.Context(c)
    43  	err := user.WriteServerFile(s, ctx, "username", "password", "outfile.blah")
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	c.Assert(filepath.IsAbs(s.serverFilename), jc.IsTrue)
    46  	c.Assert(s.serverFilename, gc.Equals, filepath.Join(ctx.Dir, "outfile.blah"))
    47  }
    48  
    49  func (s *CommonSuite) TestFileContent(c *gc.C) {
    50  	ctx := testing.Context(c)
    51  	err := user.WriteServerFile(s, ctx, "username", "password", "outfile.blah")
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	s.assertServerFileMatches(c, s.serverFilename, "username", "password")
    54  }
    55  
    56  func (s *CommonSuite) TestWriteServerFileBadUser(c *gc.C) {
    57  	ctx := testing.Context(c)
    58  	err := user.WriteServerFile(s, ctx, "bad user", "password", "outfile.blah")
    59  	c.Assert(err, gc.ErrorMatches, `"bad user" is not a valid username`)
    60  }