github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/gce/google/config_credentials_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package google_test
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/json"
     9  
    10  	"github.com/juju/errors"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/provider/gce/google"
    15  )
    16  
    17  type credentialsSuite struct {
    18  	google.BaseSuite
    19  }
    20  
    21  var _ = gc.Suite(&credentialsSuite{})
    22  
    23  func (s *credentialsSuite) TestNewCredentials(c *gc.C) {
    24  	values := map[string]string{
    25  		google.OSEnvClientID:    "abc",
    26  		google.OSEnvClientEmail: "xyz@g.com",
    27  		google.OSEnvPrivateKey:  "<some-key>",
    28  		google.OSEnvProjectID:   "yup",
    29  	}
    30  	creds, err := google.NewCredentials(values)
    31  	c.Assert(err, jc.ErrorIsNil)
    32  
    33  	jsonKey := creds.JSONKey
    34  	creds.JSONKey = nil
    35  	c.Check(creds, jc.DeepEquals, &google.Credentials{
    36  		ClientID:    "abc",
    37  		ClientEmail: "xyz@g.com",
    38  		PrivateKey:  []byte("<some-key>"),
    39  		ProjectID:   "yup",
    40  	})
    41  	data := make(map[string]string)
    42  	err = json.Unmarshal(jsonKey, &data)
    43  	c.Assert(err, jc.ErrorIsNil)
    44  	c.Check(data, jc.DeepEquals, map[string]string{
    45  		"type":         "service_account",
    46  		"client_id":    "abc",
    47  		"client_email": "xyz@g.com",
    48  		"private_key":  "<some-key>",
    49  	})
    50  }
    51  
    52  func (s *credentialsSuite) TestNewCredentialsUnrecognized(c *gc.C) {
    53  	values := map[string]string{
    54  		"spam": "eggs",
    55  	}
    56  	_, err := google.NewCredentials(values)
    57  
    58  	c.Check(err, jc.Satisfies, errors.IsNotSupported)
    59  }
    60  
    61  func (s *credentialsSuite) TestNewCredentialsValidates(c *gc.C) {
    62  	values := map[string]string{
    63  		google.OSEnvClientEmail: "xyz@g.com",
    64  		google.OSEnvPrivateKey:  "<some-key>",
    65  		google.OSEnvProjectID:   "yup",
    66  	}
    67  	_, err := google.NewCredentials(values)
    68  	// This error comes from Credentials.Validate so by implication
    69  	// if we're getting this error, validation is being performed.
    70  	c.Check(err, gc.ErrorMatches, `invalid config value \(\) for "GCE_CLIENT_ID": missing ClientID`)
    71  	c.Assert(err, jc.Satisfies, google.IsInvalidConfigValueError)
    72  }
    73  
    74  func (s *credentialsSuite) TestParseJSONKey(c *gc.C) {
    75  	original := `
    76  {
    77      "private_key_id": "mnopq",
    78      "private_key": "<some-key>",
    79      "client_email": "xyz@g.com",
    80      "client_id": "abc",
    81      "project_id": "yup",
    82      "type": "service_account"
    83  }`[1:]
    84  	creds, err := google.ParseJSONKey(bytes.NewBufferString(original))
    85  	c.Assert(err, jc.ErrorIsNil)
    86  
    87  	jsonKey := creds.JSONKey
    88  	creds.JSONKey = nil
    89  	c.Check(creds, jc.DeepEquals, &google.Credentials{
    90  		ClientID:    "abc",
    91  		ClientEmail: "xyz@g.com",
    92  		PrivateKey:  []byte("<some-key>"),
    93  		ProjectID:   "yup",
    94  	})
    95  	c.Check(string(jsonKey), gc.Equals, original)
    96  }
    97  
    98  func (s *credentialsSuite) TestCredentialsValues(c *gc.C) {
    99  	original := map[string]string{
   100  		google.OSEnvClientID:    "abc",
   101  		google.OSEnvClientEmail: "xyz@g.com",
   102  		google.OSEnvPrivateKey:  "<some-key>",
   103  		google.OSEnvProjectID:   "yup",
   104  	}
   105  	creds, err := google.NewCredentials(original)
   106  	c.Assert(err, jc.ErrorIsNil)
   107  	values := creds.Values()
   108  
   109  	c.Check(values, jc.DeepEquals, original)
   110  }
   111  
   112  func (*credentialsSuite) TestValidateValid(c *gc.C) {
   113  	creds := &google.Credentials{
   114  		ClientID:    "spam",
   115  		ClientEmail: "user@mail.com",
   116  		PrivateKey:  []byte("non-empty"),
   117  	}
   118  	err := creds.Validate()
   119  
   120  	c.Check(err, jc.ErrorIsNil)
   121  }
   122  
   123  func (*credentialsSuite) TestValidateMissingID(c *gc.C) {
   124  	creds := &google.Credentials{
   125  		ClientEmail: "user@mail.com",
   126  		PrivateKey:  []byte("non-empty"),
   127  	}
   128  	err := creds.Validate()
   129  
   130  	c.Assert(err, jc.Satisfies, google.IsInvalidConfigValueError)
   131  	c.Check(err.(*google.InvalidConfigValueError).Key, gc.Equals, "GCE_CLIENT_ID")
   132  }
   133  
   134  func (*credentialsSuite) TestValidateBadEmail(c *gc.C) {
   135  	creds := &google.Credentials{
   136  		ClientID:    "spam",
   137  		ClientEmail: "bad_email",
   138  		PrivateKey:  []byte("non-empty"),
   139  	}
   140  	err := creds.Validate()
   141  
   142  	c.Assert(err, jc.Satisfies, google.IsInvalidConfigValueError)
   143  	c.Check(err.(*google.InvalidConfigValueError).Key, gc.Equals, "GCE_CLIENT_EMAIL")
   144  }
   145  
   146  func (*credentialsSuite) TestValidateMissingKey(c *gc.C) {
   147  	creds := &google.Credentials{
   148  		ClientID:    "spam",
   149  		ClientEmail: "user@mail.com",
   150  	}
   151  	err := creds.Validate()
   152  
   153  	c.Assert(err, jc.Satisfies, google.IsInvalidConfigValueError)
   154  	c.Check(err.(*google.InvalidConfigValueError).Key, gc.Equals, "GCE_PRIVATE_KEY")
   155  }