| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | package azuremonitor | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/tsdb/azuremonitor/azcredentials" | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tsdb/azuremonitor/deprecated" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types" | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Test_httpCliProvider(t *testing.T) { | 
					
						
							|  |  |  | 	cfg := &setting.Cfg{} | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name                string | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 		route               types.AzRoute | 
					
						
							|  |  |  | 		model               types.DatasourceInfo | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 		expectedMiddlewares int | 
					
						
							|  |  |  | 		Err                 require.ErrorAssertionFunc | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 			name: "creates an HTTP client with a middleware due to the scope", | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 			route: types.AzRoute{ | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 				URL:    "http://route", | 
					
						
							|  |  |  | 				Scopes: []string{"http://route/.default"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 			model: types.DatasourceInfo{ | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 				Credentials: &azcredentials.AzureClientSecretCredentials{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expectedMiddlewares: 1, | 
					
						
							|  |  |  | 			Err:                 require.NoError, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "creates an HTTP client with a middleware due to an app key", | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 			route: types.AzRoute{ | 
					
						
							|  |  |  | 				URL:    deprecated.AzAppInsights.URL, | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 				Scopes: []string{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 			model: types.DatasourceInfo{ | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 				Credentials: &azcredentials.AzureClientSecretCredentials{}, | 
					
						
							|  |  |  | 				DecryptedSecureJSONData: map[string]string{ | 
					
						
							|  |  |  | 					"appInsightsApiKey": "foo", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 			expectedMiddlewares: 1, | 
					
						
							|  |  |  | 			Err:                 require.NoError, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "creates an HTTP client without a middleware", | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 			route: types.AzRoute{ | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 				URL:    "http://route", | 
					
						
							|  |  |  | 				Scopes: []string{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2022-03-02 22:41:07 +08:00
										 |  |  | 			model: types.DatasourceInfo{ | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 				Credentials: &azcredentials.AzureClientSecretCredentials{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expectedMiddlewares: 0, | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 			Err:                 require.NoError, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		t.Run(tt.name, func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 			m, err := getMiddlewares(tt.route, tt.model, cfg) | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Cannot test that the cli middleware works properly since the azcore sdk
 | 
					
						
							|  |  |  | 			// rejects the TLS certs (if provided)
 | 
					
						
							| 
									
										
										
										
											2021-07-16 18:47:26 +08:00
										 |  |  | 			if len(m) != tt.expectedMiddlewares { | 
					
						
							|  |  |  | 				t.Errorf("Unexpected middlewares: %v", m) | 
					
						
							| 
									
										
										
										
											2021-07-05 18:20:12 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |