-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_store_test.go
69 lines (55 loc) · 1.45 KB
/
client_store_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package oauth2xorm
import (
"context"
"fmt"
"github.com/go-oauth2/oauth2/v4/models"
_ "github.com/go-sql-driver/mysql"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"testing"
"xorm.io/xorm"
)
const TBOauthClient = "oauth2_client"
func TestClientStoreCreate(t *testing.T) {
db, _ := xorm.NewEngine("mysql", dsn)
store, _ := NewClientStore(db, WithClientStoreTableName(TBOauthClient))
clientInfo := &models.Client{
ID: "1_1",
Secret: "2_2",
Domain: "3_3",
UserID: "4_4",
}
err := store.Create(clientInfo)
assert.Nil(t, err)
// tearDown
stmt := fmt.Sprintf("delete from %s where id = ?", TBOauthClient)
_, _ = db.Exec(stmt, clientInfo.ID)
}
func TestClientStoreGetByID(t *testing.T) {
// Arrange
db, _ := xorm.NewEngine("mysql", dsn)
clientInfo := models.Client{
ID: "1_1",
Secret: "2_2",
Domain: "3_3",
UserID: "4_4",
}
data, err := jsoniter.Marshal(clientInfo)
_, _ = db.Exec(fmt.Sprintf("INSERT INTO %s (id, secret, domain, data) VALUES (?,?,?,?)", TBOauthClient),
clientInfo.ID,
clientInfo.Secret,
clientInfo.Domain,
string(data),
)
// Act
store, _ := NewClientStore(db, WithClientStoreTableName(TBOauthClient))
ctx := context.Background()
ci, err := store.GetByID(ctx, "1_1")
// Assert
assert.Nil(t, err)
assert.Equal(t, ci.GetID(), "1_1")
// TearDown
// tearDown
stmt := fmt.Sprintf("delete from %s where id = ?", TBOauthClient)
_, _ = db.Exec(stmt, clientInfo.ID)
}