Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into issue55
Browse files Browse the repository at this point in the history
  • Loading branch information
ronoaldo committed Nov 27, 2023
2 parents f00c34b + ded6012 commit 6981d70
Show file tree
Hide file tree
Showing 39 changed files with 1,208 additions and 293 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: build

on:
push:
tags:
- 'v*'

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/[email protected]
with:
go-version: 1.19

- name: Cache Go modules
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 changes: 0 additions & 34 deletions .github/workflows/tag.yml

This file was deleted.

13 changes: 8 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ jobs:

steps:
- name: Install Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4.1.0
with:
go-version: 1.17.x
go-version: 1.19

- name: Checkout code
uses: actions/[email protected]
uses: actions/[email protected]

- name: Test
run: |
go test ./... -coverprofile=profile.cov
- uses: shogo82148/[email protected]
go test -p 1 ./... -coverprofile=profile.cov
- uses: shogo82148/[email protected]
with:
path-to-profile: profile.cov
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/mtdb
17 changes: 17 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
before:
hooks:
- go mod tidy
builds:
- main: ./cmd/mtdb
env:
- CGO_ENABLED=1
targets:
- linux_amd64
ldflags:
- -s -w -extldflags=-static
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
79 changes: 71 additions & 8 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package auth

import (
"archive/zip"
"bufio"
"bytes"
"database/sql"
"encoding/json"
"fmt"

"github.com/minetest-go/mtdb/types"
Expand Down Expand Up @@ -42,14 +46,14 @@ const (
Descending OrderDirectionType = "desc"
)

var orderColumns = map[string]bool{
string(LastLogin): true,
string(Name): true,
var orderColumns = map[OrderColumnType]bool{
LastLogin: true,
Name: true,
}

var orderDirections = map[string]bool{
string(Ascending): true,
string(Descending): true,
var orderDirections = map[OrderDirectionType]bool{
Ascending: true,
Descending: true,
}

type AuthSearch struct {
Expand Down Expand Up @@ -77,9 +81,9 @@ func (repo *AuthRepository) buildWhereClause(fields string, s *AuthSearch) (stri
i++
}

if s.OrderColumn != nil && orderColumns[string(*s.OrderColumn)] {
if s.OrderColumn != nil && orderColumns[*s.OrderColumn] {
order := Ascending
if s.OrderDirection != nil && orderDirections[string(*s.OrderDirection)] {
if s.OrderDirection != nil && orderDirections[*s.OrderDirection] {
order = *s.OrderDirection
}

Expand Down Expand Up @@ -136,3 +140,62 @@ func (repo *AuthRepository) Delete(id int64) error {
_, err := repo.db.Exec("delete from auth where id = $1", id)
return err
}

func (repo *AuthRepository) DeleteAll() error {
_, err := repo.db.Exec("delete from auth")
return err
}

func (repo *AuthRepository) Export(z *zip.Writer) error {
w, err := z.Create("auth.json")
if err != nil {
return err
}
enc := json.NewEncoder(w)

rows, err := repo.db.Query("select id,name,password,last_login from auth")
if err != nil {
return err
}
defer rows.Close()

for rows.Next() {
e := &AuthEntry{}
err = rows.Scan(&e.ID, &e.Name, &e.Password, &e.LastLogin)
if err != nil {
return err
}

err = enc.Encode(e)
if err != nil {
return err
}
}

return nil
}

func (repo *AuthRepository) Import(z *zip.Reader) error {
f, err := z.Open("auth.json")
if err != nil {
return err
}
defer f.Close()

sc := bufio.NewScanner(f)
for sc.Scan() {
dc := json.NewDecoder(bytes.NewReader(sc.Bytes()))
e := &AuthEntry{}
err = dc.Decode(e)
if err != nil {
return err
}

_, err := repo.db.Exec("insert into auth(id,name,password,last_login) values($1,$2,$3,$4)", e.ID, e.Name, e.Password, e.LastLogin)
if err != nil {
return err
}
}

return nil
}
4 changes: 2 additions & 2 deletions auth/auth_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"database/sql"
"testing"

_ "modernc.org/sqlite"
_ "github.com/mattn/go-sqlite3"

"github.com/minetest-go/mtdb/auth"
"github.com/minetest-go/mtdb/types"
Expand All @@ -13,7 +13,7 @@ import (

func TestMigrateAuthSQlite(t *testing.T) {
// open db
db, err := sql.Open("sqlite", ":memory:")
db, err := sql.Open("sqlite3", ":memory:")
assert.NoError(t, err)

assert.NoError(t, auth.MigrateAuthDB(db, types.DATABASE_SQLITE))
Expand Down
10 changes: 5 additions & 5 deletions auth/auth_sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

_ "modernc.org/sqlite"
_ "github.com/mattn/go-sqlite3"

"github.com/minetest-go/mtdb/auth"
"github.com/minetest-go/mtdb/types"
Expand All @@ -15,7 +15,7 @@ import (

func TestEmptySQliteRepo(t *testing.T) {
// open db
db, err := sql.Open("sqlite", ":memory:")
db, err := sql.Open("sqlite3", ":memory:")
assert.NoError(t, err)
repo := auth.NewAuthRepository(db, types.DATABASE_SQLITE)
assert.NotNil(t, repo)
Expand All @@ -33,7 +33,7 @@ func TestSQliteRepo(t *testing.T) {
copyFileContents("testdata/auth.wal.sqlite", dbfile.Name())

// open db
db, err := sql.Open("sqlite", "file:"+dbfile.Name())
db, err := sql.Open("sqlite3", "file:"+dbfile.Name())
assert.NoError(t, err)
repo := auth.NewAuthRepository(db, types.DATABASE_SQLITE)
assert.NotNil(t, repo)
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestSQlitePrivRepo(t *testing.T) {
copyFileContents("testdata/auth.wal.sqlite", dbfile.Name())

// open db
db, err := sql.Open("sqlite", "file:"+dbfile.Name())
db, err := sql.Open("sqlite3", "file:"+dbfile.Name())
assert.NoError(t, err)
repo := auth.NewPrivilegeRepository(db, types.DATABASE_SQLITE)
assert.NotNil(t, repo)
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestSqliteAuthRepo(t *testing.T) {
dbfile, err := os.CreateTemp(os.TempDir(), "auth.sqlite")
assert.NoError(t, err)
assert.NotNil(t, dbfile)
db, err := sql.Open("sqlite", "file:"+dbfile.Name())
db, err := sql.Open("sqlite3", "file:"+dbfile.Name())
assert.NoError(t, err)
assert.NoError(t, auth.MigrateAuthDB(db, types.DATABASE_SQLITE))
assert.NoError(t, wal.EnableWAL(db))
Expand Down
52 changes: 52 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package auth_test

import (
"archive/zip"
"bytes"
"os"
"testing"

"github.com/minetest-go/mtdb/auth"
Expand Down Expand Up @@ -76,9 +79,11 @@ func testAuthRepository(t *testing.T, auth_repo *auth.AuthRepository, priv_repo
}
assert.Error(t, auth_repo.Create(auth2))

// update
e.LastLogin = 456
assert.NoError(t, auth_repo.Update(e))

// check update
e, err = auth_repo.GetByUsername("test")
assert.NoError(t, err)
assert.NotNil(t, e)
Expand All @@ -87,17 +92,64 @@ func testAuthRepository(t *testing.T, auth_repo *auth.AuthRepository, priv_repo
assert.Equal(t, 456, e.LastLogin)
assert.NotNil(t, e.ID)

// add priv
priv := &auth.PrivilegeEntry{
ID: *e.ID,
Privilege: "interact",
}
assert.NoError(t, priv_repo.Create(priv))

// check update priv
privs, err := priv_repo.GetByID(*e.ID)
assert.NoError(t, err)
assert.Equal(t, 1, len(privs))

// export
buf := bytes.NewBuffer([]byte{})
w := zip.NewWriter(buf)
err = auth_repo.Export(w)
assert.NoError(t, err)
err = priv_repo.Export(w)
assert.NoError(t, err)
err = w.Close()
assert.NoError(t, err)
zipfile, err := os.CreateTemp(os.TempDir(), "auth.zip")
assert.NoError(t, err)
f, err := os.Create(zipfile.Name())
assert.NoError(t, err)
count, err = f.Write(buf.Bytes())
assert.NoError(t, err)
assert.True(t, count > 0)

// delete priv
assert.NoError(t, priv_repo.Delete(*e.ID, "interact"))

// delete auth
assert.NoError(t, auth_repo.Delete(*e.ID))

// delete all
assert.NoError(t, auth_repo.DeleteAll())

// import
z, err := zip.OpenReader(zipfile.Name())
assert.NoError(t, err)
err = auth_repo.Import(&z.Reader)
assert.NoError(t, err)
err = priv_repo.Import(&z.Reader)
assert.NoError(t, err)

// check import
e, err = auth_repo.GetByUsername("test")
assert.NoError(t, err)
assert.NotNil(t, e)
assert.Equal(t, "test", e.Name)
assert.Equal(t, "blah", e.Password)
assert.Equal(t, 456, e.LastLogin)
assert.NotNil(t, e.ID)

// check imported priv
privs, err = priv_repo.GetByID(*e.ID)
assert.NoError(t, err)
assert.Equal(t, 1, len(privs))

}
Loading

0 comments on commit 6981d70

Please sign in to comment.