Goimport sort, invite tests
This commit is contained in:
parent
30d4fdeca8
commit
11cade0e51
|
@ -7,12 +7,14 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/endpoints"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/endpoints"
|
||||
|
||||
"whitelistmanager/store"
|
||||
)
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
"whitelistmanager/store"
|
||||
)
|
||||
|
||||
|
|
72
invite/invite_test.go
Normal file
72
invite/invite_test.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package invite_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"whitelistmanager/invite"
|
||||
mock_store "whitelistmanager/mocks/store"
|
||||
"whitelistmanager/store"
|
||||
)
|
||||
|
||||
func TestInvite(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
st := mock_store.NewMockStorer(ctrl)
|
||||
inm := invite.NewManager(st)
|
||||
|
||||
user := store.User{Id: "1"}
|
||||
|
||||
server := store.Server{
|
||||
Id: "1",
|
||||
Owner: user,
|
||||
}
|
||||
|
||||
inv := store.Invite{
|
||||
Token: "foo",
|
||||
Server: server,
|
||||
Uses: 1,
|
||||
Unlimited: false,
|
||||
}
|
||||
|
||||
t.Run("generate invite successfully", func(t *testing.T) {
|
||||
st.EXPECT().GetServer(server.Id).Return(server, nil)
|
||||
st.EXPECT().GetInvite(gomock.Any()).Return(store.Invite{}, sql.ErrNoRows)
|
||||
st.EXPECT().SaveInvite(gomock.Any()).Return(nil)
|
||||
|
||||
token, err := inm.Create(inv, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token == "" {
|
||||
t.Errorf("expected non empty string, but got empty string")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no logged uses of invite", func(t *testing.T) {
|
||||
st.EXPECT().InviteLog(inv).Return([]store.InviteLog{}, nil)
|
||||
uses, err := inm.RemainingUses(inv)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if uses != inv.Uses {
|
||||
t.Errorf("remaining uses is not correctly calculated: got %v want %v",
|
||||
uses, inv.Uses)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("one logged use of invite", func(t *testing.T) {
|
||||
st.EXPECT().InviteLog(inv).Return(make([]store.InviteLog, 1), nil)
|
||||
uses, err := inm.RemainingUses(inv)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if uses != 0 {
|
||||
t.Errorf("remaining uses is not correctly calculated: got %v want %v",
|
||||
uses, 0)
|
||||
}
|
||||
})
|
||||
}
|
4
main.go
4
main.go
|
@ -1,9 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/alexedwards/flow"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/alexedwards/flow"
|
||||
|
||||
"whitelistmanager/invite"
|
||||
"whitelistmanager/minecraft"
|
||||
"whitelistmanager/store"
|
||||
|
|
|
@ -3,7 +3,9 @@ package minecraft
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
mcrcon "github.com/Kelwing/mc-rcon"
|
||||
|
||||
"whitelistmanager/store"
|
||||
)
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ package mock_invite
|
|||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
store "whitelistmanager/store"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
|
|
|
@ -6,6 +6,7 @@ package mock_minecraft
|
|||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
store "whitelistmanager/store"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
|
|
|
@ -6,6 +6,7 @@ package mock_store
|
|||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
store "whitelistmanager/store"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
|
|
|
@ -4,11 +4,12 @@ import (
|
|||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
//go:embed database.sql
|
||||
|
|
|
@ -8,10 +8,12 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/alexedwards/flow"
|
||||
"github.com/google/uuid"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/alexedwards/flow"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"whitelistmanager/auth"
|
||||
"whitelistmanager/invite"
|
||||
"whitelistmanager/minecraft"
|
||||
|
|
|
@ -6,13 +6,15 @@ import (
|
|||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/alexedwards/flow"
|
||||
"github.com/golang/mock/gomock"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/flow"
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"whitelistmanager/invite"
|
||||
mock_invite "whitelistmanager/mocks/invite"
|
||||
mock_minecraft "whitelistmanager/mocks/minecraft"
|
||||
|
|
Loading…
Reference in a new issue