Welcome to mirror list, hosted at ThFree Co, Russian Federation.

domains_test.go « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8618df38b077d6ba016f731921492f3b070cc2a1 (plain)
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
package source

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/config"
	"gitlab.com/gitlab-org/gitlab-pages/internal/domain"
	"gitlab.com/gitlab-org/gitlab-pages/internal/source/disk"
)

func TestNewDomains(t *testing.T) {
	validCfg := config.GitLab{
		InternalServer:     "https://gitlab.com",
		APISecretKey:       []byte("abc"),
		ClientHTTPTimeout:  time.Second,
		JWTTokenExpiration: time.Second,
	}

	tests := []struct {
		name            string
		source          string
		config          config.GitLab
		expectedErr     string
		expectGitlabNil bool
		expectDiskNil   bool
	}{
		{
			name:        "no_source_config",
			source:      "",
			expectedErr: "invalid option for -domain-config-source: \"\"",
		},
		{
			name:        "invalid_source_config",
			source:      "invalid",
			expectedErr: "invalid option for -domain-config-source: \"invalid\"",
		},
		{
			name:            "disk_source",
			source:          "disk",
			expectGitlabNil: true,
			expectDiskNil:   false,
		},
		{
			name:            "auto_without_api_config",
			source:          "auto",
			expectGitlabNil: true,
			expectDiskNil:   false,
		},
		{
			name:            "auto_with_api_config",
			source:          "auto",
			config:          validCfg,
			expectGitlabNil: false,
			expectDiskNil:   false,
		},
		{
			name:          "gitlab_source_success",
			source:        "gitlab",
			config:        validCfg,
			expectDiskNil: true,
		},
		{
			name:   "gitlab_source_no_url",
			source: "gitlab",
			config: func() config.GitLab {
				cfg := validCfg
				cfg.InternalServer = ""

				return cfg
			}(),
			expectedErr: "GitLab API URL or API secret has not been provided",
		},
		{
			name:   "gitlab_source_no_secret",
			source: "gitlab",
			config: func() config.GitLab {
				cfg := validCfg
				cfg.APISecretKey = []byte{}

				return cfg
			}(),
			expectedErr: "GitLab API URL or API secret has not been provided",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			domains, err := NewDomains(tt.source, &tt.config)
			if tt.expectedErr != "" {
				require.EqualError(t, err, tt.expectedErr)
				return
			}
			require.NoError(t, err)

			require.Equal(t, tt.expectGitlabNil, domains.gitlab == nil, "mismatch gitlab nil")
			require.Equal(t, tt.expectDiskNil, domains.disk == nil, "mismatch disk nil")
		})
	}
}

func TestGetDomain(t *testing.T) {
	t.Run("when requesting an existing domain for gitlab source", func(t *testing.T) {
		testDomain := "new-source-test.gitlab.io"

		newSource := NewMockSource()
		newSource.On("GetDomain", testDomain).
			Return(&domain.Domain{Name: testDomain}, nil).
			Once()
		defer newSource.AssertExpectations(t)

		domains := newTestDomains(t, newSource, sourceGitlab)

		domain, err := domains.GetDomain(context.Background(), testDomain)
		require.NoError(t, err)
		require.NotNil(t, domain)
	})

	t.Run("when requesting an existing domain for auto source", func(t *testing.T) {
		testDomain := "new-source-test.gitlab.io"

		newSource := NewMockSource()
		newSource.On("GetDomain", testDomain).
			Return(&domain.Domain{Name: testDomain}, nil).
			Once()
		newSource.On("IsReady").Return(true).Once()
		defer newSource.AssertExpectations(t)

		domains := newTestDomains(t, newSource, sourceAuto)

		domain, err := domains.GetDomain(context.Background(), testDomain)
		require.NoError(t, err)
		require.NotNil(t, domain)
	})

	t.Run("when requesting a domain that doesn't exist for gitlab source", func(t *testing.T) {
		newSource := NewMockSource()
		newSource.On("GetDomain", "does-not-exist.test.io").
			Return(nil, nil).
			Once()

		defer newSource.AssertExpectations(t)

		domains := newTestDomains(t, newSource, sourceGitlab)

		domain, err := domains.GetDomain(context.Background(), "does-not-exist.test.io")
		require.NoError(t, err)
		require.Nil(t, domain)
	})
}

func newTestDomains(t *testing.T, gitlabSource *MockSource, config configSource) *Domains {
	t.Helper()

	return &Domains{
		configSource: config,
		gitlab:       gitlabSource,
		disk:         disk.New(),
	}
}