2019-03-11 16:56:48 +00:00
|
|
|
package webroot
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-24 19:07:20 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-03-14 10:49:02 +00:00
|
|
|
)
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
func TestHTTPProvider(t *testing.T) {
|
2016-03-14 10:49:02 +00:00
|
|
|
webroot := "webroot"
|
|
|
|
domain := "domain"
|
|
|
|
token := "token"
|
|
|
|
keyAuth := "keyAuth"
|
|
|
|
challengeFilePath := webroot + "/.well-known/acme-challenge/" + token
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
require.NoError(t, os.MkdirAll(webroot+"/.well-known/acme-challenge", 0o777))
|
2016-03-14 10:49:02 +00:00
|
|
|
defer os.RemoveAll(webroot)
|
|
|
|
|
2016-03-16 10:32:09 +00:00
|
|
|
provider, err := NewHTTPProvider(webroot)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
err = provider.Present(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-14 10:49:02 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
if _, err = os.Stat(challengeFilePath); os.IsNotExist(err) {
|
2016-03-14 10:49:02 +00:00
|
|
|
t.Error("Challenge file was not created in webroot")
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
var data []byte
|
2021-08-25 09:44:11 +00:00
|
|
|
data, err = os.ReadFile(challengeFilePath)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-03-14 10:49:02 +00:00
|
|
|
dataStr := string(data)
|
2018-09-24 19:07:20 +00:00
|
|
|
assert.Equal(t, keyAuth, dataStr)
|
2016-03-14 10:49:02 +00:00
|
|
|
|
|
|
|
err = provider.CleanUp(domain, token, keyAuth)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-14 10:49:02 +00:00
|
|
|
}
|