github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/credentialvalidator/credentialvalidator_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package credentialvalidator_test
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	names "gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/api/base"
    14  	apitesting "github.com/juju/juju/api/base/testing"
    15  	"github.com/juju/juju/api/credentialvalidator"
    16  	"github.com/juju/juju/apiserver/common"
    17  	"github.com/juju/juju/apiserver/params"
    18  )
    19  
    20  var _ = gc.Suite(&CredentialValidatorSuite{})
    21  
    22  type CredentialValidatorSuite struct {
    23  	testing.IsolationSuite
    24  }
    25  
    26  func (s *CredentialValidatorSuite) TestModelCredential(c *gc.C) {
    27  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    28  		c.Check(objType, gc.Equals, "CredentialValidator")
    29  		c.Check(request, gc.Equals, "ModelCredential")
    30  		c.Check(arg, gc.IsNil)
    31  		c.Assert(result, gc.FitsTypeOf, &params.ModelCredential{})
    32  		*(result.(*params.ModelCredential)) = params.ModelCredential{
    33  			Model:           modelTag.String(),
    34  			CloudCredential: credentialTag.String(),
    35  			Exists:          true,
    36  			Valid:           true,
    37  		}
    38  		return nil
    39  	})
    40  
    41  	client := credentialvalidator.NewFacade(apiCaller)
    42  	found, exists, err := client.ModelCredential()
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Assert(exists, jc.IsTrue)
    45  	c.Assert(found, gc.DeepEquals, base.StoredCredential{CloudCredential: "cloud/user/credential", Valid: true})
    46  }
    47  
    48  func (s *CredentialValidatorSuite) TestModelCredentialIsNotNeeded(c *gc.C) {
    49  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    50  		*(result.(*params.ModelCredential)) = params.ModelCredential{
    51  			Model:  modelTag.String(),
    52  			Exists: false,
    53  		}
    54  		return nil
    55  	})
    56  
    57  	client := credentialvalidator.NewFacade(apiCaller)
    58  	_, exists, err := client.ModelCredential()
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(exists, jc.IsFalse)
    61  }
    62  
    63  func (s *CredentialValidatorSuite) TestModelCredentialInvalidCredentialTag(c *gc.C) {
    64  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    65  		*(result.(*params.ModelCredential)) = params.ModelCredential{
    66  			Model:           modelTag.String(),
    67  			Exists:          true,
    68  			CloudCredential: "some-invalid-cloud-credential-tag-as-string",
    69  		}
    70  		return nil
    71  	})
    72  
    73  	client := credentialvalidator.NewFacade(apiCaller)
    74  	_, exists, err := client.ModelCredential()
    75  	c.Assert(err, gc.ErrorMatches, `"some-invalid-cloud-credential-tag-as-string" is not a valid tag`)
    76  	c.Assert(exists, jc.IsFalse)
    77  }
    78  
    79  func (s *CredentialValidatorSuite) TestModelCredentialCallError(c *gc.C) {
    80  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    81  		return errors.New("foo")
    82  	})
    83  
    84  	client := credentialvalidator.NewFacade(apiCaller)
    85  	_, _, err := client.ModelCredential()
    86  	c.Assert(err, gc.ErrorMatches, "foo")
    87  }
    88  
    89  func (s *CredentialValidatorSuite) TestWatchCredentialError(c *gc.C) {
    90  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    91  		*(result.(*params.NotifyWatchResult)) = params.NotifyWatchResult{Error: &params.Error{Message: "foo"}}
    92  		return nil
    93  	})
    94  
    95  	client := credentialvalidator.NewFacade(apiCaller)
    96  	_, err := client.WatchCredential(credentialID)
    97  	c.Assert(err, gc.ErrorMatches, "foo")
    98  }
    99  
   100  func (s *CredentialValidatorSuite) TestWatchCredentialCallError(c *gc.C) {
   101  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   102  		return errors.New("foo")
   103  	})
   104  
   105  	client := credentialvalidator.NewFacade(apiCaller)
   106  	_, err := client.WatchCredential(credentialID)
   107  	c.Assert(err, gc.ErrorMatches, "foo")
   108  }
   109  
   110  var (
   111  	modelUUID = "e5757df7-c86a-4835-84bc-7174af535d25"
   112  	modelTag  = names.NewModelTag(modelUUID)
   113  
   114  	credentialID  = "cloud/user/credential"
   115  	credentialTag = names.NewCloudCredentialTag(credentialID)
   116  )
   117  
   118  func (s *CredentialValidatorSuite) TestInvalidateModelCredential(c *gc.C) {
   119  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   120  		c.Check(objType, gc.Equals, "CredentialValidator")
   121  		c.Check(request, gc.Equals, "InvalidateModelCredential")
   122  		c.Assert(arg, gc.Equals, params.InvalidateCredentialArg{Reason: "auth fail"})
   123  		c.Assert(result, gc.FitsTypeOf, &params.ErrorResult{})
   124  		*(result.(*params.ErrorResult)) = params.ErrorResult{}
   125  		return nil
   126  	})
   127  
   128  	client := credentialvalidator.NewFacade(apiCaller)
   129  	err := client.InvalidateModelCredential("auth fail")
   130  	c.Assert(err, jc.ErrorIsNil)
   131  }
   132  
   133  func (s *CredentialValidatorSuite) TestInvalidateModelCredentialBackendFailure(c *gc.C) {
   134  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   135  		*(result.(*params.ErrorResult)) = params.ErrorResult{Error: common.ServerError(errors.New("boom"))}
   136  		return nil
   137  	})
   138  
   139  	client := credentialvalidator.NewFacade(apiCaller)
   140  	err := client.InvalidateModelCredential("")
   141  	c.Assert(err, gc.ErrorMatches, "boom")
   142  }
   143  
   144  func (s *CredentialValidatorSuite) TestInvalidateModelCredentialError(c *gc.C) {
   145  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   146  		return errors.New("foo")
   147  	})
   148  
   149  	client := credentialvalidator.NewFacade(apiCaller)
   150  	err := client.InvalidateModelCredential("")
   151  	c.Assert(err, gc.ErrorMatches, "foo")
   152  }
   153  
   154  func (s *CredentialValidatorSuite) TestWatchModelCredentialError(c *gc.C) {
   155  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   156  		*(result.(*params.NotifyWatchResult)) = params.NotifyWatchResult{Error: &params.Error{Message: "foo"}}
   157  		return nil
   158  	})
   159  	client := credentialvalidator.NewFacade(apitesting.BestVersionCaller{apiCaller, 2})
   160  	_, err := client.WatchModelCredential()
   161  	c.Assert(err, gc.ErrorMatches, "foo")
   162  }
   163  
   164  func (s *CredentialValidatorSuite) TestWatchModelCredentialCallError(c *gc.C) {
   165  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   166  		return errors.New("foo")
   167  	})
   168  
   169  	client := credentialvalidator.NewFacade(apitesting.BestVersionCaller{apiCaller, 2})
   170  	_, err := client.WatchModelCredential()
   171  	c.Assert(err, gc.ErrorMatches, "foo")
   172  }
   173  
   174  func (s *CredentialValidatorSuite) TestWatchModelCredentialCallV1(c *gc.C) {
   175  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
   176  		return errors.New("foo")
   177  	})
   178  
   179  	client := credentialvalidator.NewFacade(apitesting.BestVersionCaller{apiCaller, 1})
   180  	_, err := client.WatchModelCredential()
   181  	c.Assert(err, gc.ErrorMatches, "WatchModelCredential on CredentialValidator v1 not supported")
   182  }