2 // ========================LICENSE_START=================================
5 // Copyright (C) 2023: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
26 "github.com/stretchr/testify/assert"
27 "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
30 func TestValidateClientIdNotPresent(t *testing.T) {
31 accessTokenUnderTest := AccessTokenReq{}
32 valid, err := accessTokenUnderTest.Validate()
34 assert.Equal(t, false, valid)
35 assert.Equal(t, AccessTokenErrErrorInvalidRequest, err.Error)
36 assert.Equal(t, "Invalid request", *err.ErrorDescription)
39 func TestValidateGrantType(t *testing.T) {
40 accessTokenUnderTest := AccessTokenReq{
42 GrantType: AccessTokenReqGrantType(""),
44 valid, err := accessTokenUnderTest.Validate()
46 assert.Equal(t, false, valid)
47 assert.Equal(t, AccessTokenErrErrorInvalidGrant, err.Error)
48 assert.Equal(t, "Invalid value for grant_type", *err.ErrorDescription)
50 accessTokenUnderTest.GrantType = AccessTokenReqGrantType("client_credentials")
51 valid, err = accessTokenUnderTest.Validate()
52 assert.Equal(t, true, valid)
55 func TestValidateScopeNotValid(t *testing.T) {
56 scope := "scope#aefId:path"
57 accessTokenUnderTest := AccessTokenReq{
59 GrantType: ("client_credentials"),
62 valid, err := accessTokenUnderTest.Validate()
64 assert.Equal(t, false, valid)
65 assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
66 assert.Equal(t, "Scope should start with 3gpp", *err.ErrorDescription)
68 scope = "3gpp#aefId:path"
69 accessTokenUnderTest.Scope = &scope
70 valid, err = accessTokenUnderTest.Validate()
71 assert.Equal(t, true, valid)
74 func TestValidateScopeMalformed(t *testing.T) {
76 accessTokenUnderTest := AccessTokenReq{
78 GrantType: ("client_credentials"),
81 valid, err := accessTokenUnderTest.Validate()
83 assert.Equal(t, false, valid)
84 assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
85 assert.Equal(t, "Malformed scope", *err.ErrorDescription)
88 accessTokenUnderTest.Scope = &scope
89 valid, err = accessTokenUnderTest.Validate()
90 assert.Equal(t, false, valid)
91 assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
92 assert.Equal(t, "Malformed scope", *err.ErrorDescription)
94 scope = "3gpp#aefId:path"
95 accessTokenUnderTest.Scope = &scope
96 valid, err = accessTokenUnderTest.Validate()
97 assert.Equal(t, true, valid)
100 func TestValidateServiceSecurity(t *testing.T) {
101 serviceSecurityUnderTest := ServiceSecurity{}
103 err := serviceSecurityUnderTest.Validate()
104 assert.NotNil(t, err)
105 assert.Contains(t, err.Error(), "missing")
106 assert.Contains(t, err.Error(), "notificationDestination")
108 serviceSecurityUnderTest.NotificationDestination = "invalid dest"
109 err = serviceSecurityUnderTest.Validate()
110 if assert.Error(t, err) {
111 assert.Contains(t, err.Error(), "invalid")
112 assert.Contains(t, err.Error(), "notificationDestination")
115 serviceSecurityUnderTest.NotificationDestination = "http://golang.cafe/"
116 err = serviceSecurityUnderTest.Validate()
117 assert.NotNil(t, err)
118 assert.Contains(t, err.Error(), "missing")
119 assert.Contains(t, err.Error(), "SecurityInfo")
121 serviceSecurityUnderTest.SecurityInfo = []SecurityInformation{
123 PrefSecurityMethods: []publishserviceapi.SecurityMethod{
124 publishserviceapi.SecurityMethodOAUTH,
128 err = serviceSecurityUnderTest.Validate()
132 func TestValidatePrefSecurityMethodsNotPresent(t *testing.T) {
133 securityInfoUnderTest := SecurityInformation{}
134 err := securityInfoUnderTest.Validate()
136 assert.NotNil(t, err)
137 assert.Contains(t, err.Error(), "missing")
138 assert.Contains(t, err.Error(), "PrefSecurityMethods")
140 securityInfoUnderTest.PrefSecurityMethods = []publishserviceapi.SecurityMethod{
141 publishserviceapi.SecurityMethodOAUTH,
143 err = securityInfoUnderTest.Validate()