github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/environs/testing/credentials.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/cloud"
    13  	"github.com/juju/juju/environs"
    14  )
    15  
    16  // AssertProviderAuthTypes asserts that the given provider has credential
    17  // schemas for exactly the specified set of authentication types.
    18  func AssertProviderAuthTypes(c *gc.C, p environs.EnvironProvider, expectedAuthTypes ...cloud.AuthType) {
    19  	var authTypes []cloud.AuthType
    20  	for authType := range p.CredentialSchemas() {
    21  		authTypes = append(authTypes, authType)
    22  	}
    23  	c.Assert(authTypes, jc.SameContents, expectedAuthTypes)
    24  }
    25  
    26  // AssertProviderCredentialsValid asserts that the given provider is
    27  // able to validate the given authentication type and credential
    28  // attributes; and that removing any one of the attributes will cause
    29  // the validation to fail.
    30  func AssertProviderCredentialsValid(c *gc.C, p environs.EnvironProvider, authType cloud.AuthType, attrs map[string]string) {
    31  	schema, ok := p.CredentialSchemas()[authType]
    32  	c.Assert(ok, jc.IsTrue, gc.Commentf("missing schema for %q auth-type", authType))
    33  	validate := func(attrs map[string]string) error {
    34  		_, err := schema.Finalize(attrs, func(path string) ([]byte, error) {
    35  			return []byte("contentsOf(" + path + ")"), nil
    36  		})
    37  		return err
    38  	}
    39  
    40  	err := validate(attrs)
    41  	c.Assert(err, jc.ErrorIsNil)
    42  
    43  	for excludedKey := range attrs {
    44  		field, _ := schema.Attribute(excludedKey)
    45  		if field.Optional {
    46  			continue
    47  		}
    48  		reducedAttrs := make(map[string]string)
    49  		for key, value := range attrs {
    50  			if key != excludedKey {
    51  				reducedAttrs[key] = value
    52  			}
    53  		}
    54  		err := validate(reducedAttrs)
    55  		if field.FileAttr != "" {
    56  			c.Assert(err, gc.ErrorMatches, fmt.Sprintf(
    57  				`either %q or %q must be specified`, excludedKey, field.FileAttr),
    58  			)
    59  		} else {
    60  			c.Assert(err, gc.ErrorMatches, excludedKey+": expected string, got nothing")
    61  		}
    62  	}
    63  }
    64  
    65  // AssertProviderCredentialsAttributesHidden asserts that the provider
    66  // credentials schema for the given provider and authentication type
    67  // marks the specified attributes (and only those attributes) as being
    68  // hidden.
    69  func AssertProviderCredentialsAttributesHidden(c *gc.C, p environs.EnvironProvider, authType cloud.AuthType, expectedHidden ...string) {
    70  	var hidden []string
    71  	schema, ok := p.CredentialSchemas()[authType]
    72  	c.Assert(ok, jc.IsTrue, gc.Commentf("missing schema for %q auth-type", authType))
    73  	for _, field := range schema {
    74  		if field.Hidden {
    75  			hidden = append(hidden, field.Name)
    76  		}
    77  	}
    78  	c.Assert(hidden, jc.SameContents, expectedHidden)
    79  }