[OOM-K8S-CERT-EXTERNAL-PROVIDER] Refactor provider code
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / model / sign_certificate_model_factory_test.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package model
22
23 import (
24         "context"
25         "testing"
26
27         cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1"
28         "github.com/stretchr/testify/assert"
29         "sigs.k8s.io/controller-runtime/pkg/client/fake"
30
31         "onap.org/oom-certservice/k8s-external-provider/src/testdata"
32 )
33
34 const (
35         revisionAnnotation                 = "cert-manager.io/certificate-revision"
36         certificateConfigurationAnnotation = "kubectl.kubernetes.io/last-applied-configuration"
37         testPrivateKeyData                 = "test-private-key"
38         testCertificateData                = "test-certificate"
39 )
40
41 func Test_shouldCreateCertificateModelWithCorrectParameters(t *testing.T) {
42         request := new(cmapi.CertificateRequest)
43         request.ObjectMeta.Annotations = map[string]string{
44                 revisionAnnotation:                 "2",
45                 certificateConfigurationAnnotation: testdata.OldCertificateConfig,
46         }
47         request.Spec.Request = testdata.CsrBytes
48         fakeClient := fake.NewFakeClientWithScheme(testdata.GetScheme(), testdata.GetValidCertificateSecret())
49
50         signCertModel, err := CreateSignCertificateModel(fakeClient, request, *new(context.Context), testdata.PkBytes)
51
52         assert.Nil(t, err)
53         assert.NotNil(t, signCertModel)
54         assert.NotNil(t, signCertModel.FilteredCsr)
55         assert.Equal(t, testdata.PkBytes, signCertModel.PrivateKeyBytes)
56         assert.Equal(t, request, signCertModel.CertificateRequest)
57         assert.Equal(t, []byte(testCertificateData), signCertModel.OldCertificateBytes)
58         assert.Equal(t, []byte(testPrivateKeyData), signCertModel.OldPrivateKeyBytes)
59 }