-
Notifications
You must be signed in to change notification settings - Fork 55
/
policy_test.go
103 lines (84 loc) · 2.67 KB
/
policy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2024 The Witness Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package policy
import (
"context"
"errors"
"testing"
"github.com/in-toto/go-witness/dsse"
"github.com/in-toto/witness/internal/archivista"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
// Mock archivista client
type ArchivistaClienterMock struct {
mock.Mock
archivista.Clienter
}
func (m *ArchivistaClienterMock) Download(ctx context.Context, path string) (dsse.Envelope, error) {
args := m.Called()
return dsse.Envelope{}, args.Error(1)
}
// Define test suite
type UTPolicySuite struct {
suite.Suite
mockedAC *ArchivistaClienterMock
}
func TestUTPolicySuite(t *testing.T) {
suite.Run(t, new(UTPolicySuite))
}
// Setup test suite
func (ut *UTPolicySuite) SetupTest() {
ut.mockedAC = &ArchivistaClienterMock{}
}
// Test LoadPolicy with file
func (ut *UTPolicySuite) TestLoadPolicyFile() {
ctx := context.Background()
policy := "../../test/policy-hello-signed.json"
// Load policy from file
policyEnvelope, err := LoadPolicy(ctx, policy, nil)
ut.NoError(err)
ut.NotNil(policyEnvelope)
}
// Test LoadPolicy with file not found
func (ut *UTPolicySuite) TestLoadPolicyFileNotFound() {
ctx := context.Background()
policy := "notfound"
// Load policy from file
_, err := LoadPolicy(ctx, policy, nil)
ut.Error(err)
ut.Contains(err.Error(), "no such file or directory")
}
// Test LoadPolicy with archivista
func (ut *UTPolicySuite) TestLoadPolicyArchivista() {
ctx := context.Background()
policy := "testgitoid"
// Mock archivista client
ut.mockedAC.On("Download").Return(dsse.Envelope{}, nil)
// Load policy from archivista
policyEnvelope, err := LoadPolicy(ctx, policy, ut.mockedAC)
ut.NoError(err)
ut.NotNil(policyEnvelope)
}
// Test LoadPolicy with archivista not found
func (ut *UTPolicySuite) TestLoadPolicyArchivistaNotFound() {
ctx := context.Background()
policy := "testgitoid"
// Mock archivista client
ut.mockedAC.On("Download").Return(dsse.Envelope{}, errors.New("not found"))
// Load policy from archivista
_, err := LoadPolicy(ctx, policy, ut.mockedAC)
ut.Error(err)
ut.Contains(err.Error(), "failed to fetch policy from archivista")
}