mirror of
https://github.com/writefreely/writefreely
synced 2024-11-10 11:24:13 +00:00
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package writefreely
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
func TestOAuthDatastore(t *testing.T) {
|
|
if !runMySQLTests() {
|
|
t.Skip("skipping mysql tests")
|
|
}
|
|
withTestDB(t, func(db *sql.DB) {
|
|
ctx := context.Background()
|
|
ds := &datastore{
|
|
DB: db,
|
|
driverName: "",
|
|
}
|
|
|
|
state, err := ds.GenerateOAuthState(ctx, "test", "development")
|
|
assert.NoError(t, err)
|
|
assert.Len(t, state, 24)
|
|
|
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `oauth_client_state` WHERE `state` = ? AND `used` = false", state)
|
|
|
|
_, _, err = ds.ValidateOAuthState(ctx, state)
|
|
assert.NoError(t, err)
|
|
|
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `oauth_client_state` WHERE `state` = ? AND `used` = true", state)
|
|
|
|
var localUserID int64 = 99
|
|
var remoteUserID = "100"
|
|
err = ds.RecordRemoteUserID(ctx, localUserID, remoteUserID, "test", "test", "access_token_a")
|
|
assert.NoError(t, err)
|
|
|
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `users_oauth` WHERE `user_id` = ? AND `remote_user_id` = ? AND access_token = 'access_token_a'", localUserID, remoteUserID)
|
|
|
|
err = ds.RecordRemoteUserID(ctx, localUserID, remoteUserID, "test", "test", "access_token_b")
|
|
assert.NoError(t, err)
|
|
|
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `users_oauth` WHERE `user_id` = ? AND `remote_user_id` = ? AND access_token = 'access_token_b'", localUserID, remoteUserID)
|
|
|
|
countRows(t, ctx, db, 1, "SELECT COUNT(*) FROM `users_oauth`")
|
|
|
|
foundUserID, err := ds.GetIDForRemoteUser(ctx, remoteUserID, "test", "test")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, localUserID, foundUserID)
|
|
})
|
|
}
|