violet/servers/api/api_test.go

128 lines
3.8 KiB
Go
Raw Normal View History

package api
2023-06-04 22:28:48 +01:00
import (
2023-07-22 01:11:47 +01:00
"github.com/1f349/violet/servers/conf"
"github.com/1f349/violet/utils"
"github.com/1f349/violet/utils/fake"
2023-06-04 22:28:48 +01:00
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewApiServer_Compile(t *testing.T) {
apiConf := &conf.Conf{
Domains: &fake.Domains{},
2023-06-04 22:28:48 +01:00
Acme: utils.NewAcmeChallenge(),
Signer: fake.SnakeOilProv,
2023-06-04 22:28:48 +01:00
}
f := &fake.Compilable{}
2024-02-16 01:41:42 +00:00
srv := NewApiServer(apiConf, utils.MultiCompilable{f}, nil)
2023-06-04 22:28:48 +01:00
req, err := http.NewRequest(http.MethodPost, "https://example.com/compile", nil)
assert.NoError(t, err)
rec := httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res := rec.Result()
assert.Equal(t, http.StatusForbidden, res.StatusCode)
assert.False(t, f.Done)
2023-06-04 22:28:48 +01:00
req.Header.Set("Authorization", "Bearer "+fake.GenSnakeOilKey("violet:compile"))
2023-06-04 22:28:48 +01:00
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusAccepted, res.StatusCode)
assert.True(t, f.Done)
2023-06-04 22:28:48 +01:00
}
func TestNewApiServer_AcmeChallenge_Put(t *testing.T) {
apiConf := &conf.Conf{
Domains: &fake.Domains{},
2023-06-04 22:28:48 +01:00
Acme: utils.NewAcmeChallenge(),
Signer: fake.SnakeOilProv,
2023-06-04 22:28:48 +01:00
}
2024-02-16 01:41:42 +00:00
srv := NewApiServer(apiConf, utils.MultiCompilable{}, nil)
acmeKey := fake.GenSnakeOilKey("violet:acme-challenge")
2023-06-04 22:28:48 +01:00
// Valid domain
req, err := http.NewRequest(http.MethodPut, "https://example.com/acme-challenge/example.com/123/123abc", nil)
assert.NoError(t, err)
rec := httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res := rec.Result()
assert.Equal(t, http.StatusForbidden, res.StatusCode)
req.Header.Set("Authorization", "Bearer "+acmeKey)
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusAccepted, res.StatusCode)
assert.Equal(t, "123abc", apiConf.Acme.Get("example.com", "123"))
// Invalid domain
req, err = http.NewRequest(http.MethodPut, "https://example.com/acme-challenge/notexample.com/123/123abc", nil)
assert.NoError(t, err)
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusForbidden, res.StatusCode)
req.Header.Set("Authorization", "Bearer "+acmeKey)
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.Equal(t, "Invalid ACME challenge domain", res.Header.Get("X-Violet-Error"))
}
func TestNewApiServer_AcmeChallenge_Delete(t *testing.T) {
apiConf := &conf.Conf{
Domains: &fake.Domains{},
2023-06-04 22:28:48 +01:00
Acme: utils.NewAcmeChallenge(),
Signer: fake.SnakeOilProv,
2023-06-04 22:28:48 +01:00
}
2024-02-16 01:41:42 +00:00
srv := NewApiServer(apiConf, utils.MultiCompilable{}, nil)
acmeKey := fake.GenSnakeOilKey("violet:acme-challenge")
2023-06-04 22:28:48 +01:00
// Valid domain
req, err := http.NewRequest(http.MethodDelete, "https://example.com/acme-challenge/example.com/123", nil)
assert.NoError(t, err)
rec := httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res := rec.Result()
assert.Equal(t, http.StatusForbidden, res.StatusCode)
req.Header.Set("Authorization", "Bearer "+acmeKey)
apiConf.Acme.Put("example.com", "123", "123abc")
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusAccepted, res.StatusCode)
assert.Equal(t, "", apiConf.Acme.Get("example.com", "123"))
// Invalid domain
req, err = http.NewRequest(http.MethodDelete, "https://example.com/acme-challenge/notexample.com/123", nil)
assert.NoError(t, err)
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusForbidden, res.StatusCode)
req.Header.Set("Authorization", "Bearer "+acmeKey)
rec = httptest.NewRecorder()
srv.Handler.ServeHTTP(rec, req)
res = rec.Result()
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.Equal(t, "Invalid ACME challenge domain", res.Header.Get("X-Violet-Error"))
}