github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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) TestNewCredentialsEmpty(c *gc.C) { 53 creds, err := google.NewCredentials(nil) 54 c.Assert(err, jc.ErrorIsNil) 55 56 c.Check(creds, jc.DeepEquals, &google.Credentials{}) 57 } 58 59 func (s *credentialsSuite) TestNewCredentialsUnrecognized(c *gc.C) { 60 values := map[string]string{ 61 "spam": "eggs", 62 } 63 _, err := google.NewCredentials(values) 64 65 c.Check(err, jc.Satisfies, errors.IsNotSupported) 66 } 67 68 func (s *credentialsSuite) TestParseJSONKey(c *gc.C) { 69 original := ` 70 { 71 "private_key_id": "mnopq", 72 "private_key": "<some-key>", 73 "client_email": "xyz@g.com", 74 "client_id": "abc", 75 "project_id": "yup", 76 "type": "service_account" 77 }`[1:] 78 creds, err := google.ParseJSONKey(bytes.NewBufferString(original)) 79 c.Assert(err, jc.ErrorIsNil) 80 81 jsonKey := creds.JSONKey 82 creds.JSONKey = nil 83 c.Check(creds, jc.DeepEquals, &google.Credentials{ 84 ClientID: "abc", 85 ClientEmail: "xyz@g.com", 86 PrivateKey: []byte("<some-key>"), 87 ProjectID: "yup", 88 }) 89 c.Check(string(jsonKey), gc.Equals, original) 90 } 91 92 func (s *credentialsSuite) TestCredentialsValues(c *gc.C) { 93 original := map[string]string{ 94 google.OSEnvClientID: "abc", 95 google.OSEnvClientEmail: "xyz@g.com", 96 google.OSEnvPrivateKey: "<some-key>", 97 google.OSEnvProjectID: "yup", 98 } 99 creds, err := google.NewCredentials(original) 100 c.Assert(err, jc.ErrorIsNil) 101 values := creds.Values() 102 103 c.Check(values, jc.DeepEquals, original) 104 } 105 106 func (*credentialsSuite) TestValidateValid(c *gc.C) { 107 creds := &google.Credentials{ 108 ClientID: "spam", 109 ClientEmail: "user@mail.com", 110 PrivateKey: []byte("non-empty"), 111 } 112 err := creds.Validate() 113 114 c.Check(err, jc.ErrorIsNil) 115 } 116 117 func (*credentialsSuite) TestValidateMissingID(c *gc.C) { 118 creds := &google.Credentials{ 119 ClientEmail: "user@mail.com", 120 PrivateKey: []byte("non-empty"), 121 } 122 err := creds.Validate() 123 124 c.Assert(err, jc.Satisfies, google.IsInvalidConfigValue) 125 c.Check(err.(*google.InvalidConfigValue).Key, gc.Equals, "GCE_CLIENT_ID") 126 } 127 128 func (*credentialsSuite) TestValidateBadEmail(c *gc.C) { 129 creds := &google.Credentials{ 130 ClientID: "spam", 131 ClientEmail: "bad_email", 132 PrivateKey: []byte("non-empty"), 133 } 134 err := creds.Validate() 135 136 c.Assert(err, jc.Satisfies, google.IsInvalidConfigValue) 137 c.Check(err.(*google.InvalidConfigValue).Key, gc.Equals, "GCE_CLIENT_EMAIL") 138 } 139 140 func (*credentialsSuite) TestValidateMissingKey(c *gc.C) { 141 creds := &google.Credentials{ 142 ClientID: "spam", 143 ClientEmail: "user@mail.com", 144 } 145 err := creds.Validate() 146 147 c.Assert(err, jc.Satisfies, google.IsInvalidConfigValue) 148 c.Check(err.(*google.InvalidConfigValue).Key, gc.Equals, "GCE_PRIVATE_KEY") 149 }