summaryrefslogtreecommitdiff
path: root/services/worker/internal/fetcher/authentication_test.go
blob: bc840b9c8c1a59d6fd2fe31ed132ab2ec62105ef (plain) (blame)
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
package fetcher

import (
	"encoding/base64"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"net/http"
	"testing"
)

func TestApplyBearerAuthentication(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "bearer",
		AuthenticationValue: "my-secret-token",
	})

	require.NoError(test, authenticationError)
	assert.Equal(test, "Bearer my-secret-token", request.Header.Get("Authorization"))
}

func TestApplyBasicAuthentication(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "basic",
		AuthenticationValue: "user:pass",
	})

	require.NoError(test, authenticationError)

	expectedEncoded := base64.StdEncoding.EncodeToString([]byte("user:pass"))

	assert.Equal(test, "Basic "+expectedEncoded, request.Header.Get("Authorization"))
}

func TestApplyQueryParamAuthentication(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed?existing=value", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "query_param",
		AuthenticationValue: "api_key=abc123",
	})

	require.NoError(test, authenticationError)
	assert.Equal(test, "abc123", request.URL.Query().Get("api_key"))
	assert.Equal(test, "value", request.URL.Query().Get("existing"))
}

func TestApplyEmptyAuthenticationType(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "",
		AuthenticationValue: "",
	})

	require.NoError(test, authenticationError)
	assert.Empty(test, request.Header.Get("Authorization"))
}

func TestApplyUnsupportedAuthenticationType(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "oauth2",
		AuthenticationValue: "token",
	})

	assert.Error(test, authenticationError)
	assert.Contains(test, authenticationError.Error(), "unsupported")
}

func TestApplyQueryParamInvalidFormat(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "query_param",
		AuthenticationValue: "no-equals-sign",
	})

	assert.Error(test, authenticationError)
	assert.Contains(test, authenticationError.Error(), "param_name=value")
}

func TestApplyQueryParamWithEqualsInValue(test *testing.T) {
	request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil)
	authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{
		AuthenticationType:  "query_param",
		AuthenticationValue: "token=abc=def",
	})

	require.NoError(test, authenticationError)
	assert.Equal(test, "abc=def", request.URL.Query().Get("token"))
}