mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:24:03 +03:00
Add test for generate-secret command
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type BaseTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
b base
|
||||
}
|
||||
|
||||
func (suite *BaseTestSuite) SetupTest() {
|
||||
suite.b = base{}
|
||||
}
|
||||
|
||||
func (suite *BaseTestSuite) TestReadConfigNok() {
|
||||
suite.Error(suite.b.ReadConfig(filepath.Join("testdata", "unknown"), "dev"))
|
||||
}
|
||||
|
||||
func (suite *BaseTestSuite) TestReadConfig() {
|
||||
suite.NoError(suite.b.ReadConfig(filepath.Join("testdata", "minimal.toml"), "dev"))
|
||||
}
|
||||
|
||||
func TestBase(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &BaseTestSuite{})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/9seconds/mtg/v2/mtglib"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type GenerateSecretTestSuite struct {
|
||||
CommonTestSuite
|
||||
}
|
||||
|
||||
func (suite *GenerateSecretTestSuite) SetupTest() {
|
||||
suite.CommonTestSuite.SetupTest()
|
||||
|
||||
suite.cli.GenerateSecret.HostName = "google.com"
|
||||
}
|
||||
|
||||
func (suite *GenerateSecretTestSuite) TestDefault() {
|
||||
output := suite.CaptureStdout(func() {
|
||||
suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
|
||||
})
|
||||
suite.True(strings.HasPrefix(output, "7"))
|
||||
|
||||
secret, err := mtglib.ParseSecret(output)
|
||||
suite.NoError(err)
|
||||
suite.True(secret.Valid())
|
||||
suite.Equal("google.com", secret.Host)
|
||||
}
|
||||
|
||||
func (suite *GenerateSecretTestSuite) TestHex() {
|
||||
suite.cli.GenerateSecret.Hex = true
|
||||
|
||||
output := suite.CaptureStdout(func() {
|
||||
suite.NoError(suite.cli.GenerateSecret.Run(suite.cli, "dev"))
|
||||
})
|
||||
suite.True(strings.HasPrefix(output, "ee"))
|
||||
|
||||
secret, err := mtglib.ParseSecret(output)
|
||||
suite.NoError(err)
|
||||
suite.True(secret.Valid())
|
||||
suite.Equal("google.com", secret.Host)
|
||||
}
|
||||
|
||||
func TestGenerateSecret(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &GenerateSecretTestSuite{})
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/9seconds/mtg/v2/cli"
|
||||
"github.com/9seconds/mtg/v2/mtglib/network"
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
type NetworkMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (n *NetworkMock) Dial(network, address string) (net.Conn, error) {
|
||||
args := n.Called(network, address)
|
||||
|
||||
return args.Get(0).(net.Conn), args.Error(1)
|
||||
}
|
||||
|
||||
func (n *NetworkMock) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
args := n.Called(ctx, network, address)
|
||||
|
||||
return args.Get(0).(net.Conn), args.Error(1)
|
||||
}
|
||||
|
||||
func (n *NetworkMock) DNSResolve(network, hostname string) ([]string, error) {
|
||||
args := n.Called(network, hostname)
|
||||
|
||||
return args.Get(0).([]string), args.Error(1)
|
||||
}
|
||||
|
||||
func (n *NetworkMock) MakeHTTPClient(dialFunc network.DialFunc) *http.Client {
|
||||
return n.Called(dialFunc).Get(0).(*http.Client)
|
||||
}
|
||||
|
||||
func (n *NetworkMock) IdleTimeout() time.Duration {
|
||||
return n.Called().Get(0).(time.Duration)
|
||||
}
|
||||
|
||||
func (n *NetworkMock) HTTPTimeout() time.Duration {
|
||||
return n.Called().Get(0).(time.Duration)
|
||||
}
|
||||
|
||||
type CommonTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
cli *cli.CLI
|
||||
networkMock *NetworkMock
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func (suite *CommonTestSuite) SetupTest() {
|
||||
suite.networkMock = &NetworkMock{}
|
||||
suite.httpClient = &http.Client{}
|
||||
suite.cli = &cli.CLI{}
|
||||
|
||||
httpmock.ActivateNonDefault(suite.httpClient)
|
||||
|
||||
suite.networkMock.
|
||||
On("MakeHTTPClient", mock.Anything).
|
||||
Maybe().
|
||||
Return(suite.httpClient)
|
||||
}
|
||||
|
||||
func (suite *CommonTestSuite) TearDownTest() {
|
||||
suite.networkMock.AssertExpectations(suite.T())
|
||||
httpmock.DeactivateAndReset()
|
||||
}
|
||||
|
||||
func (suite *CommonTestSuite) CaptureStdout(callback func()) string {
|
||||
return suite.captureOutput(&os.Stdout, callback)
|
||||
}
|
||||
|
||||
func (suite *CommonTestSuite) CaptureStderr(callback func()) string {
|
||||
return suite.captureOutput(&os.Stderr, callback)
|
||||
}
|
||||
|
||||
func (suite *CommonTestSuite) captureOutput(filefp **os.File, callback func()) string {
|
||||
oldFp := *filefp
|
||||
|
||||
defer func() {
|
||||
*filefp = oldFp
|
||||
}()
|
||||
|
||||
reader, writer, _ := os.Pipe()
|
||||
buf := &bytes.Buffer{}
|
||||
closeChan := make(chan bool)
|
||||
|
||||
go func() {
|
||||
io.Copy(buf, reader) // nolint: errcheck
|
||||
close(closeChan)
|
||||
}()
|
||||
|
||||
*filefp = writer
|
||||
|
||||
callback()
|
||||
|
||||
writer.Close()
|
||||
<-closeChan
|
||||
|
||||
return strings.TrimSpace(buf.String())
|
||||
}
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
secret = "7mqFMMq3P2Tvvt_rPx5qhmFnb29nbGUuY29t"
|
||||
bind-to = "0.0.0.0:80"
|
||||
Reference in New Issue
Block a user