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"
29 func TestValidateClientIdNotPresent(t *testing.T) {
30 accessTokenUnderTest := AccessTokenReq{}
31 valid, err := accessTokenUnderTest.Validate()
33 assert.Equal(t, false, valid)
34 assert.Equal(t, AccessTokenErrErrorInvalidRequest, err.Error)
35 assert.Equal(t, "Invalid request", *err.ErrorDescription)
38 func TestValidateGrantType(t *testing.T) {
39 accessTokenUnderTest := AccessTokenReq{
41 GrantType: AccessTokenReqGrantType(""),
43 valid, err := accessTokenUnderTest.Validate()
45 assert.Equal(t, false, valid)
46 assert.Equal(t, AccessTokenErrErrorInvalidGrant, err.Error)
47 assert.Equal(t, "Invalid value for grant_type", *err.ErrorDescription)
49 accessTokenUnderTest.GrantType = AccessTokenReqGrantType("client_credentials")
50 valid, err = accessTokenUnderTest.Validate()
51 assert.Equal(t, true, valid)
54 func TestValidateScopeNotValid(t *testing.T) {
55 scope := "scope#aefId:path"
56 accessTokenUnderTest := AccessTokenReq{
58 GrantType: ("client_credentials"),
61 valid, err := accessTokenUnderTest.Validate()
63 assert.Equal(t, false, valid)
64 assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
65 assert.Equal(t, "Scope should start with 3gpp", *err.ErrorDescription)
67 scope = "3gpp#aefId:path"
68 accessTokenUnderTest.Scope = &scope
69 valid, err = accessTokenUnderTest.Validate()
70 assert.Equal(t, true, valid)
73 func TestValidateScopeMalformed(t *testing.T) {
75 accessTokenUnderTest := AccessTokenReq{
77 GrantType: ("client_credentials"),
80 valid, err := accessTokenUnderTest.Validate()
82 assert.Equal(t, false, valid)
83 assert.Equal(t, AccessTokenErrErrorInvalidScope, err.Error)
84 assert.Equal(t, "Malformed scope", *err.ErrorDescription)
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)
93 scope = "3gpp#aefId:path"
94 accessTokenUnderTest.Scope = &scope
95 valid, err = accessTokenUnderTest.Validate()
96 assert.Equal(t, true, valid)