0515d06133a01177bbb73fc2b420ec7839da807b
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typevalidation_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 )
28
29 func TestValidateClientIdNotPresent(t *testing.T) {
30         accessTokenUnderTest := AccessTokenReq{}
31         valid, err := accessTokenUnderTest.Validate()
32
33         assert.Equal(t, false, valid)
34         assert.Equal(t, AccessTokenErrErrorInvalidRequest, err.Error)
35         assert.Equal(t, "Invalid request", *err.ErrorDescription)
36 }
37
38 func TestValidateGrantType(t *testing.T) {
39         accessTokenUnderTest := AccessTokenReq{
40                 ClientId:  "clientId",
41                 GrantType: AccessTokenReqGrantType(""),
42         }
43         valid, err := accessTokenUnderTest.Validate()
44
45         assert.Equal(t, false, valid)
46         assert.Equal(t, AccessTokenErrErrorInvalidGrant, err.Error)
47         assert.Equal(t, "Invalid value for grant_type", *err.ErrorDescription)
48
49         accessTokenUnderTest.GrantType = AccessTokenReqGrantType("client_credentials")
50         valid, err = accessTokenUnderTest.Validate()
51         assert.Equal(t, true, valid)
52 }
53
54 func TestValidateScopeNotValid(t *testing.T) {
55         scope := "scope#aefId:path"
56         accessTokenUnderTest := AccessTokenReq{
57                 ClientId:  "clientId",
58                 GrantType: ("client_credentials"),
59                 Scope:     &scope,
60         }
61         valid, err := accessTokenUnderTest.Validate()
62
63         assert.Equal(t, false, valid)
64         assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
65         assert.Equal(t, "Scope should start with 3gpp", *err.ErrorDescription)
66
67         scope = "3gpp#aefId:path"
68         accessTokenUnderTest.Scope = &scope
69         valid, err = accessTokenUnderTest.Validate()
70         assert.Equal(t, true, valid)
71 }
72
73 func TestValidateScopeMalformed(t *testing.T) {
74         scope := "3gpp"
75         accessTokenUnderTest := AccessTokenReq{
76                 ClientId:  "clientId",
77                 GrantType: ("client_credentials"),
78                 Scope:     &scope,
79         }
80         valid, err := accessTokenUnderTest.Validate()
81
82         assert.Equal(t, false, valid)
83         assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
84         assert.Equal(t, "Malformed scope", *err.ErrorDescription)
85
86         scope = "3gpp#aefId"
87         accessTokenUnderTest.Scope = &scope
88         valid, err = accessTokenUnderTest.Validate()
89         assert.Equal(t, false, valid)
90         assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
91         assert.Equal(t, "Malformed scope", *err.ErrorDescription)
92
93         scope = "3gpp#aefId:path"
94         accessTokenUnderTest.Scope = &scope
95         valid, err = accessTokenUnderTest.Validate()
96         assert.Equal(t, true, valid)
97 }