11package akamai
22
33import (
4+ "fmt"
45 "testing"
56
67 "github.com/1Password/shell-plugins/sdk"
78 "github.com/1Password/shell-plugins/sdk/plugintest"
89 "github.com/1Password/shell-plugins/sdk/schema/fieldname"
10+ "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
912)
1013
1114func TestAPIClientCredentialsProvisioner (t * testing.T ) {
@@ -30,6 +33,66 @@ func TestAPIClientCredentialsProvisioner(t *testing.T) {
3033 })
3134}
3235
36+ func TestConfigFileRejectsLineBreaks (t * testing.T ) {
37+ validFields := map [sdk.FieldName ]string {
38+ fieldname .ClientSecret : "abcdE23FNkBxy456z25qx9Yp5CPUxlEfQeTDkfh4QA=I" ,
39+ fieldname .Host : "akab-lmn789n2k53w7qrs-nfkxaa4lfk3kd6ym.luna.akamaiapis.net" ,
40+ fieldname .AccessToken : "akab-zyx987xa6osbli4k-e7jf5ikib5jknes3" ,
41+ fieldname .ClientToken : "akab-nomoflavjuc4422e-fa2xznerxrm3teg7" ,
42+ }
43+
44+ for field , validValue := range validFields {
45+ for name , lineBreak := range map [string ]string {
46+ "line feed" : "\n " ,
47+ "carriage return" : "\r " ,
48+ } {
49+ t .Run (fmt .Sprintf ("%s/%s" , field , name ), func (t * testing.T ) {
50+ fields := make (map [sdk.FieldName ]string , len (validFields ))
51+ for key , value := range validFields {
52+ fields [key ] = value
53+ }
54+ fields [field ] = validValue + lineBreak + "debug = true"
55+
56+ contents , err := configFile (sdk.ProvisionInput {ItemFields : fields })
57+
58+ assert .Nil (t , contents )
59+ require .EqualError (t , err , fmt .Sprintf ("line breaks are not allowed in the Akamai %q field" , field ))
60+ assert .NotContains (t , err .Error (), "debug = true" )
61+ })
62+ }
63+ }
64+ }
65+
66+ func TestConfigFileTrimsSurroundingWhitespace (t * testing.T ) {
67+ validFields := map [sdk.FieldName ]string {
68+ fieldname .ClientSecret : "abcdE23FNkBxy456z25qx9Yp5CPUxlEfQeTDkfh4QA=I" ,
69+ fieldname .Host : "akab-lmn789n2k53w7qrs-nfkxaa4lfk3kd6ym.luna.akamaiapis.net" ,
70+ fieldname .AccessToken : "akab-zyx987xa6osbli4k-e7jf5ikib5jknes3" ,
71+ fieldname .ClientToken : "akab-nomoflavjuc4422e-fa2xznerxrm3teg7" ,
72+ }
73+ expected , err := configFile (sdk.ProvisionInput {ItemFields : validFields })
74+ require .NoError (t , err )
75+
76+ for name , pad := range map [string ]func (string ) string {
77+ "trailing line feed" : func (v string ) string { return v + "\n " },
78+ "trailing CRLF" : func (v string ) string { return v + "\r \n " },
79+ "leading line feed" : func (v string ) string { return "\n " + v },
80+ "surrounding spaces, tabs" : func (v string ) string { return " \t " + v + "\t " },
81+ } {
82+ t .Run (name , func (t * testing.T ) {
83+ fields := make (map [sdk.FieldName ]string , len (validFields ))
84+ for key , value := range validFields {
85+ fields [key ] = pad (value )
86+ }
87+
88+ contents , err := configFile (sdk.ProvisionInput {ItemFields : fields })
89+
90+ require .NoError (t , err )
91+ assert .Equal (t , string (expected ), string (contents ))
92+ })
93+ }
94+ }
95+
3396func TestAPIClientCredentialsImporter (t * testing.T ) {
3497 plugintest .TestImporter (t , APIClientCredentials ().Importer , map [string ]plugintest.ImportCase {
3598 "config file with single credential" : {
0 commit comments