@@ -2,9 +2,10 @@ package fetchers
22
33import (
44 "context"
5+ "fmt"
56 "net/http"
7+ "net/http/httptest"
68
7- "github.com/onsi/gomega/ghttp"
89 "github.com/stretchr/testify/assert"
910 "github.com/stretchr/testify/require"
1011 v1 "k8s.io/api/core/v1"
@@ -16,32 +17,92 @@ import (
1617 . "github.com/onsi/ginkgo/v2"
1718)
1819
19- var _ = Describe ("Fetching dashboards from URL" , func () {
20+ const (
21+ basicAuthUsername = "root"
22+ basicAuthPassword = "secret"
23+ )
24+
25+ func getCredentials (secretName string ) (* v1.Secret , * v1beta1.GrafanaContentURLAuthorization ) {
26+ GinkgoHelper ()
27+
28+ const (
29+ usernameKey = "USERNAME"
30+ passwordKey = "PASSWORD"
31+ )
32+
33+ secret := & v1.Secret {
34+ ObjectMeta : metav1.ObjectMeta {
35+ Name : secretName ,
36+ Namespace : "default" ,
37+ },
38+ StringData : map [string ]string {
39+ usernameKey : basicAuthUsername ,
40+ passwordKey : basicAuthPassword ,
41+ },
42+ }
43+
44+ urlAuthorization := & v1beta1.GrafanaContentURLAuthorization {
45+ BasicAuth : & v1beta1.GrafanaContentURLBasicAuth {
46+ Username : & v1.SecretKeySelector {
47+ LocalObjectReference : v1.LocalObjectReference {
48+ Name : secretName ,
49+ },
50+ Key : usernameKey ,
51+ Optional : nil ,
52+ },
53+ Password : & v1.SecretKeySelector {
54+ LocalObjectReference : v1.LocalObjectReference {
55+ Name : secretName ,
56+ },
57+ Key : passwordKey ,
58+ Optional : nil ,
59+ },
60+ },
61+ }
62+
63+ return secret , urlAuthorization
64+ }
65+
66+ var _ = Describe ("URL fetcher" , Ordered , func () {
2067 t := GinkgoT ()
2168
2269 want := []byte (`{"dummyField": "dummyData"}` )
2370 wantCompressed , err := cache .Gzip (want )
2471
2572 require .NoError (t , err )
2673
27- var server * ghttp.Server
74+ publicEndpoint := "/public"
75+ privateEndpoint := "/private"
2876
29- BeforeEach (func () {
30- server = ghttp .NewServer ()
77+ mux := http .NewServeMux ()
78+ mux .HandleFunc (publicEndpoint , func (w http.ResponseWriter , req * http.Request ) {
79+ w .Header ().Set ("Content-Type" , "application/json" )
80+ fmt .Fprint (w , string (want ))
81+ })
82+ mux .HandleFunc (privateEndpoint , func (w http.ResponseWriter , req * http.Request ) {
83+ username , password , ok := req .BasicAuth ()
84+ if ! ok || username != basicAuthUsername || password != basicAuthPassword {
85+ http .Error (w , "Unauthorized" , http .StatusUnauthorized )
86+ return
87+ }
88+
89+ w .Header ().Set ("Content-Type" , "application/json" )
90+ fmt .Fprint (w , string (want ))
3191 })
3292
33- When ("using no authentication" , func () {
34- BeforeEach (func () {
35- server .AppendHandlers (ghttp .CombineHandlers (
36- ghttp .RespondWith (http .StatusOK , want ),
37- ))
38- })
93+ ts := httptest .NewServer (mux )
94+ AfterAll (func () {
95+ ts .Close ()
96+ })
3997
40- It ("fetches the correct url" , func () {
98+ When ("no authentication" , func () {
99+ url := ts .URL + publicEndpoint
100+
101+ It ("successfully fetches the dashboard" , func () {
41102 dashboard := & v1beta1.GrafanaDashboard {
42103 Spec : v1beta1.GrafanaDashboardSpec {
43104 GrafanaContentSpec : v1beta1.GrafanaContentSpec {
44- URL : server . URL () ,
105+ URL : url ,
45106 },
46107 },
47108 Status : v1beta1.GrafanaDashboardStatus {},
@@ -52,65 +113,31 @@ var _ = Describe("Fetching dashboards from URL", func() {
52113
53114 assert .Equal (t , want , got )
54115 assert .Equal (t , wantCompressed , dashboard .Status .ContentCache )
55- assert .Equal (t , server . URL () , dashboard .Status .ContentURL )
116+ assert .Equal (t , url , dashboard .Status .ContentURL )
56117 assert .NotZero (t , dashboard .Status .ContentTimestamp .Time )
57118 })
58119 })
59120
60121 When ("using authentication" , func () {
61- basicAuthUsername := "admin"
62- basicAuthPassword := "admin"
63-
64- BeforeEach (func () {
65- server .AppendHandlers (ghttp .CombineHandlers (
66- ghttp .VerifyBasicAuth (basicAuthUsername , basicAuthPassword ),
67- ghttp .RespondWith (http .StatusOK , want ),
68- ))
69- })
122+ url := ts .URL + privateEndpoint
123+
124+ It ("successfully fetches the dashboard" , func () {
125+ credentialsSecret , urlAuthorization := getCredentials ("credentials" )
70126
71- It ("fetches the correct url" , func () {
72127 dashboard := & v1beta1.GrafanaDashboard {
73128 ObjectMeta : metav1.ObjectMeta {
74- Name : "test " ,
129+ Name : "url-basic-auth " ,
75130 Namespace : "default" ,
76131 },
77132 Spec : v1beta1.GrafanaDashboardSpec {
78133 GrafanaContentSpec : v1beta1.GrafanaContentSpec {
79- URL : server .URL (),
80- URLAuthorization : & v1beta1.GrafanaContentURLAuthorization {
81- BasicAuth : & v1beta1.GrafanaContentURLBasicAuth {
82- Username : & v1.SecretKeySelector {
83- LocalObjectReference : v1.LocalObjectReference {
84- Name : "credentials" ,
85- },
86- Key : "USERNAME" ,
87- Optional : nil ,
88- },
89- Password : & v1.SecretKeySelector {
90- LocalObjectReference : v1.LocalObjectReference {
91- Name : "credentials" ,
92- },
93- Key : "PASSWORD" ,
94- Optional : nil ,
95- },
96- },
97- },
134+ URL : url ,
135+ URLAuthorization : urlAuthorization ,
98136 },
99137 },
100138 Status : v1beta1.GrafanaDashboardStatus {},
101139 }
102140
103- credentialsSecret := & v1.Secret {
104- ObjectMeta : metav1.ObjectMeta {
105- Name : "credentials" ,
106- Namespace : "default" ,
107- },
108- StringData : map [string ]string {
109- "USERNAME" : "admin" ,
110- "PASSWORD" : "admin" ,
111- },
112- }
113-
114141 err = k8sClient .Create (context .Background (), credentialsSecret )
115142 require .NoError (t , err )
116143
@@ -119,7 +146,7 @@ var _ = Describe("Fetching dashboards from URL", func() {
119146
120147 assert .Equal (t , want , got )
121148 assert .Equal (t , wantCompressed , dashboard .Status .ContentCache )
122- assert .Equal (t , server . URL () , dashboard .Status .ContentURL )
149+ assert .Equal (t , url , dashboard .Status .ContentURL )
123150 assert .NotZero (t , dashboard .Status .ContentTimestamp .Time )
124151 })
125152 })
0 commit comments