File: shared_test.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (203 lines) | stat: -rw-r--r-- 3,667 bytes parent folder | download | duplicates (3)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package shared

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestGetSecretEntity(t *testing.T) {
	tests := []struct {
		name        string
		orgName     string
		envName     string
		userSecrets bool
		want        SecretEntity
		wantErr     bool
	}{
		{
			name:    "org",
			orgName: "myOrg",
			want:    Organization,
		},
		{
			name:    "env",
			envName: "myEnv",
			want:    Environment,
		},
		{
			name:        "user",
			userSecrets: true,
			want:        User,
		},
		{
			name: "defaults to repo",
			want: Repository,
		},
		{
			name:    "Errors if both org and env are set",
			orgName: "myOrg",
			envName: "myEnv",
			wantErr: true,
		},
		{
			name:        "Errors if both org and user secrets are set",
			orgName:     "myOrg",
			userSecrets: true,
			wantErr:     true,
		},
		{
			name:        "Errors if both env and user secrets are set",
			envName:     "myEnv",
			userSecrets: true,
			wantErr:     true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			entity, err := GetSecretEntity(tt.orgName, tt.envName, tt.userSecrets)

			if tt.wantErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
				assert.Equal(t, tt.want, entity)
			}
		})
	}
}

func TestGetSecretApp(t *testing.T) {
	tests := []struct {
		name    string
		app     string
		entity  SecretEntity
		want    App
		wantErr bool
	}{
		{
			name: "Actions",
			app:  "actions",
			want: Actions,
		},
		{
			name: "Codespaces",
			app:  "codespaces",
			want: Codespaces,
		},
		{
			name: "Dependabot",
			app:  "dependabot",
			want: Dependabot,
		},
		{
			name:   "Defaults to Actions for repository",
			app:    "",
			entity: Repository,
			want:   Actions,
		},
		{
			name:   "Defaults to Actions for organization",
			app:    "",
			entity: Organization,
			want:   Actions,
		},
		{
			name:   "Defaults to Actions for environment",
			app:    "",
			entity: Environment,
			want:   Actions,
		},
		{
			name:   "Defaults to Codespaces for user",
			app:    "",
			entity: User,
			want:   Codespaces,
		},
		{
			name:    "Unknown for invalid apps",
			app:     "invalid",
			want:    Unknown,
			wantErr: true,
		},
		{
			name: "case insensitive",
			app:  "ACTIONS",
			want: Actions,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			app, err := GetSecretApp(tt.app, tt.entity)
			if tt.wantErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
			}
			assert.Equal(t, tt.want, app)
		})
	}
}

func TestIsSupportedSecretEntity(t *testing.T) {
	tests := []struct {
		name                string
		app                 App
		supportedEntities   []SecretEntity
		unsupportedEntities []SecretEntity
	}{
		{
			name: "Actions",
			app:  Actions,
			supportedEntities: []SecretEntity{
				Repository,
				Organization,
				Environment,
			},
			unsupportedEntities: []SecretEntity{
				User,
				Unknown,
			},
		},
		{
			name: "Codespaces",
			app:  Codespaces,
			supportedEntities: []SecretEntity{
				User,
				Repository,
				Organization,
			},
			unsupportedEntities: []SecretEntity{
				Environment,
				Unknown,
			},
		},
		{
			name: "Dependabot",
			app:  Dependabot,
			supportedEntities: []SecretEntity{
				Repository,
				Organization,
			},
			unsupportedEntities: []SecretEntity{
				Environment,
				User,
				Unknown,
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			for _, entity := range tt.supportedEntities {
				assert.True(t, IsSupportedSecretEntity(tt.app, entity))
			}

			for _, entity := range tt.unsupportedEntities {
				assert.False(t, IsSupportedSecretEntity(tt.app, entity))
			}
		})
	}
}