generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add provisioner webhook auth tests (#136)
Added unit tests for the new webhook header checks.
- Loading branch information
1 parent
e6a8df8
commit 10db783
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAuthenticateWebhook(t *testing.T) { | ||
plugin := Plugin{ | ||
configuration: &configuration{}, | ||
} | ||
|
||
t.Run("no auth set, header not defined", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, plugin.authenticateWebhook(request)) | ||
}) | ||
|
||
t.Run("no auth set, header defined as empty", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
request.Header.Add(authHeaderKey, "") | ||
|
||
require.NoError(t, plugin.authenticateWebhook(request)) | ||
}) | ||
|
||
t.Run("no auth set, header defined as wrong value", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
request.Header.Add(authHeaderKey, "test") | ||
|
||
require.EqualError(t, plugin.authenticateWebhook(request), "unauthorized") | ||
}) | ||
|
||
plugin.configuration.ProvisioningServerWebhookSecret = "secret1" | ||
|
||
t.Run("auth set, header not defined", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
|
||
require.EqualError(t, plugin.authenticateWebhook(request), "unauthorized") | ||
}) | ||
|
||
t.Run("auth set, header defined as empty", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
request.Header.Add(authHeaderKey, "") | ||
|
||
require.EqualError(t, plugin.authenticateWebhook(request), "unauthorized") | ||
}) | ||
|
||
t.Run("auth set, header defined as wrong value", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
request.Header.Add(authHeaderKey, "test") | ||
|
||
require.EqualError(t, plugin.authenticateWebhook(request), "unauthorized") | ||
}) | ||
|
||
t.Run("auth set, header defined as right value", func(t *testing.T) { | ||
request, err := http.NewRequest(http.MethodPost, "test.domain.com", nil) | ||
require.NoError(t, err) | ||
request.Header.Add(authHeaderKey, "secret1") | ||
|
||
require.NoError(t, plugin.authenticateWebhook(request)) | ||
}) | ||
} |