github.com/Venafi/vcert/v5@v5.10.2/pkg/playbook/app/parser/writer_test.go (about)

     1  /*
     2   * Copyright 2023 Venafi, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *  http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package parser
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/suite"
    25  
    26  	"github.com/Venafi/vcert/v5/pkg/certificate"
    27  	"github.com/Venafi/vcert/v5/pkg/endpoint"
    28  	"github.com/Venafi/vcert/v5/pkg/playbook/app/domain"
    29  )
    30  
    31  type WriterSuite struct {
    32  	suite.Suite
    33  	playbook domain.Playbook
    34  }
    35  
    36  func (s *WriterSuite) SetupTest() {
    37  	s.playbook = domain.Playbook{
    38  		CertificateTasks: domain.CertificateTasks{
    39  			{
    40  				Name: "testTask",
    41  				Request: domain.PlaybookRequest{
    42  					ChainOption: certificate.ChainOptionRootFirst,
    43  					CsrOrigin:   certificate.StrServiceGeneratedCSR,
    44  					CustomFields: []certificate.CustomField{
    45  						{
    46  							Name:  fmt.Sprintf("cf_%s", RandomString(5)),
    47  							Value: RandomString(10),
    48  						},
    49  					},
    50  					KeyCurve: certificate.EllipticCurveP521,
    51  					KeyType:  certificate.KeyTypeRSA,
    52  					Subject: domain.Subject{
    53  						CommonName:   "foo.bar.123.venafi.com",
    54  						Country:      "US",
    55  						Locality:     "Salt Lake City",
    56  						Organization: "Venafi",
    57  						Province:     "Utah",
    58  					},
    59  					Zone: "Open Source\\vcert",
    60  				},
    61  				Installations: []domain.Installation{
    62  					{
    63  						Type:        domain.FormatPEM,
    64  						File:        "path/to/my/pem/folder",
    65  						AfterAction: "echo Success!",
    66  						KeyPassword: "foo123",
    67  					},
    68  				},
    69  				RenewBefore: "30d",
    70  			},
    71  		},
    72  		Config: domain.Config{
    73  			Connection: domain.Connection{
    74  				URL:             "https://foo.bar.venafi.com",
    75  				TrustBundlePath: "path/to/my/trustbundle.pem",
    76  				Credentials: domain.Authentication{
    77  					Authentication: endpoint.Authentication{
    78  						AccessToken:  "123fooBar",
    79  						RefreshToken: "456XyzABc",
    80  						IdentityProvider: &endpoint.OAuthProvider{
    81  							TokenURL: "okta.com",
    82  							Audience: "myAudience",
    83  						},
    84  					},
    85  					P12Task: "",
    86  				},
    87  			},
    88  		},
    89  		Location: fmt.Sprintf("./write_test_%s.yaml", RandomString(5)),
    90  	}
    91  }
    92  
    93  // this function executes after each test case
    94  func (s *WriterSuite) TearDownTest() {
    95  	err := os.Remove(s.playbook.Location)
    96  	s.Nil(err)
    97  }
    98  
    99  func TestWriter(t *testing.T) {
   100  	suite.Run(t, new(WriterSuite))
   101  }
   102  
   103  func (s *WriterSuite) TestWriter_WritePlaybook() {
   104  	err := WritePlaybook(s.playbook, "C:/foo/bar.yaml")
   105  	s.Error(err)
   106  
   107  	err = WritePlaybook(s.playbook, s.playbook.Location)
   108  	s.Nil(err)
   109  
   110  	pb, err := ReadPlaybook(s.playbook.Location)
   111  	s.Nil(err)
   112  	s.NotNil(pb)
   113  
   114  	s.Equal(s.playbook.Location, pb.Location)
   115  	s.Equal(s.playbook.Config.Connection.URL, pb.Config.Connection.URL)
   116  	s.Equal(s.playbook.Config.Connection.TrustBundlePath, pb.Config.Connection.TrustBundlePath)
   117  	s.Equal(s.playbook.Config.Connection.Credentials.AccessToken, pb.Config.Connection.Credentials.AccessToken)
   118  	s.Equal(s.playbook.Config.Connection.Credentials.RefreshToken, pb.Config.Connection.Credentials.RefreshToken)
   119  
   120  	task := s.playbook.CertificateTasks[0]
   121  	targetTask := pb.CertificateTasks[0]
   122  	s.Equal(task.Name, targetTask.Name)
   123  	s.Equal(task.RenewBefore, targetTask.RenewBefore)
   124  
   125  	req := task.Request
   126  	targetReq := targetTask.Request
   127  	s.Equal(req.ChainOption, targetReq.ChainOption)
   128  	s.Equal(req.CsrOrigin, targetReq.CsrOrigin)
   129  
   130  	s.Equal(req.CustomFields[0].Type, targetReq.CustomFields[0].Type)
   131  	s.Equal(req.CustomFields[0].Name, targetReq.CustomFields[0].Name)
   132  	s.Equal(req.CustomFields[0].Value, targetReq.CustomFields[0].Value)
   133  
   134  	s.Equal(req.KeyCurve, targetReq.KeyCurve)
   135  	s.Equal(req.KeyType, targetReq.KeyType)
   136  
   137  	s.Equal(req.Zone, targetReq.Zone)
   138  	s.Equal(req.Subject.CommonName, targetReq.Subject.CommonName)
   139  	s.Equal(req.Subject.Country, targetReq.Subject.Country)
   140  	s.Equal(req.Subject.Locality, targetReq.Subject.Locality)
   141  	s.Equal(req.Subject.Organization, targetReq.Subject.Organization)
   142  	s.Equal(req.Subject.Province, targetReq.Subject.Province)
   143  
   144  	inst := task.Installations[0]
   145  	targetInst := targetTask.Installations[0]
   146  
   147  	s.Equal(inst.Type, targetInst.Type)
   148  	s.Equal(inst.File, targetInst.File)
   149  	s.Equal(inst.KeyPassword, targetInst.KeyPassword)
   150  	s.Equal(inst.AfterAction, targetInst.AfterAction)
   151  }