Implementation for PUT trustedInvokers endpoint
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typeupdate_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023: Nordix Foundation
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 securityapi
22
23 import (
24         "testing"
25
26         "github.com/stretchr/testify/assert"
27         "oransc.org/nonrtric/capifcore/internal/common29122"
28         publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
29 )
30
31 func TestPrepareNewSecurityContext(t *testing.T) {
32         apiId := "app-management"
33         aefId := "aefId"
34         description := "Description"
35         services := []publishapi.ServiceAPIDescription{
36                 {
37                         AefProfiles: &[]publishapi.AefProfile{
38                                 {
39                                         AefId: aefId,
40                                         Versions: []publishapi.Version{
41                                                 {
42                                                         Resources: &[]publishapi.Resource{
43                                                                 {
44                                                                         CommType: "REQUEST_RESPONSE",
45                                                                 },
46                                                         },
47                                                 },
48                                         },
49                                         SecurityMethods: &[]publishapi.SecurityMethod{
50                                                 publishapi.SecurityMethodPKI,
51                                         },
52                                 },
53                         },
54                         ApiId:       &apiId,
55                         Description: &description,
56                 },
57         }
58
59         servSecurityUnderTest := ServiceSecurity{
60                 NotificationDestination: common29122.Uri("http://golang.cafe/"),
61                 SecurityInfo: []SecurityInformation{
62                         {
63                                 PrefSecurityMethods: []publishapi.SecurityMethod{
64                                         publishapi.SecurityMethodOAUTH,
65                                 },
66                         },
67                 },
68         }
69
70         err := servSecurityUnderTest.PrepareNewSecurityContext(services)
71
72         assert.NotNil(t, err)
73         assert.Contains(t, err.Error(), "not found ")
74         assert.Contains(t, err.Error(), "security method")
75
76         servSecurityUnderTest.SecurityInfo = []SecurityInformation{
77                 {
78                         ApiId: &apiId,
79                         AefId: &aefId,
80                         PrefSecurityMethods: []publishapi.SecurityMethod{
81                                 publishapi.SecurityMethodOAUTH,
82                         },
83                 },
84         }
85
86         servSecurityUnderTest.PrepareNewSecurityContext(services)
87         assert.Equal(t, publishapi.SecurityMethodPKI, *servSecurityUnderTest.SecurityInfo[0].SelSecurityMethod)
88
89         servSecurityUnderTest.SecurityInfo = []SecurityInformation{
90                 {
91                         ApiId: &apiId,
92                         PrefSecurityMethods: []publishapi.SecurityMethod{
93                                 publishapi.SecurityMethodOAUTH,
94                         },
95                         InterfaceDetails: &publishapi.InterfaceDescription{
96                                 SecurityMethods: &[]publishapi.SecurityMethod{
97                                         publishapi.SecurityMethodPSK,
98                                 },
99                         },
100                 },
101         }
102
103         servSecurityUnderTest.PrepareNewSecurityContext(services)
104         assert.Equal(t, publishapi.SecurityMethodPSK, *servSecurityUnderTest.SecurityInfo[0].SelSecurityMethod)
105
106 }