Goimport sort, invite tests
This commit is contained in:
parent
30d4fdeca8
commit
11cade0e51
|
@ -7,12 +7,14 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/oauth2"
|
|
||||||
"golang.org/x/oauth2/endpoints"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/endpoints"
|
||||||
|
|
||||||
"whitelistmanager/store"
|
"whitelistmanager/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"whitelistmanager/store"
|
"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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/alexedwards/flow"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/alexedwards/flow"
|
||||||
|
|
||||||
"whitelistmanager/invite"
|
"whitelistmanager/invite"
|
||||||
"whitelistmanager/minecraft"
|
"whitelistmanager/minecraft"
|
||||||
"whitelistmanager/store"
|
"whitelistmanager/store"
|
||||||
|
|
|
@ -3,7 +3,9 @@ package minecraft
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
mcrcon "github.com/Kelwing/mc-rcon"
|
mcrcon "github.com/Kelwing/mc-rcon"
|
||||||
|
|
||||||
"whitelistmanager/store"
|
"whitelistmanager/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ package mock_invite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
|
|
||||||
store "whitelistmanager/store"
|
store "whitelistmanager/store"
|
||||||
|
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
|
|
@ -6,6 +6,7 @@ package mock_minecraft
|
||||||
|
|
||||||
import (
|
import (
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
|
|
||||||
store "whitelistmanager/store"
|
store "whitelistmanager/store"
|
||||||
|
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
|
|
@ -6,6 +6,7 @@ package mock_store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
|
|
||||||
store "whitelistmanager/store"
|
store "whitelistmanager/store"
|
||||||
|
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
|
|
@ -4,11 +4,12 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"embed"
|
"embed"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/google/uuid"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed database.sql
|
//go:embed database.sql
|
||||||
|
|
|
@ -8,10 +8,12 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/alexedwards/flow"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/alexedwards/flow"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"whitelistmanager/auth"
|
"whitelistmanager/auth"
|
||||||
"whitelistmanager/invite"
|
"whitelistmanager/invite"
|
||||||
"whitelistmanager/minecraft"
|
"whitelistmanager/minecraft"
|
||||||
|
|
|
@ -6,13 +6,15 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/alexedwards/flow"
|
|
||||||
"github.com/golang/mock/gomock"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/alexedwards/flow"
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
|
|
||||||
"whitelistmanager/invite"
|
"whitelistmanager/invite"
|
||||||
mock_invite "whitelistmanager/mocks/invite"
|
mock_invite "whitelistmanager/mocks/invite"
|
||||||
mock_minecraft "whitelistmanager/mocks/minecraft"
|
mock_minecraft "whitelistmanager/mocks/minecraft"
|
||||||
|
|
Loading…
Reference in a new issue