github.com/dolthub/go-mysql-server@v0.18.0/sql/mysql_db/user_test.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package mysql_db 16 17 import ( 18 "encoding/json" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/require" 23 24 "github.com/dolthub/go-mysql-server/sql" 25 ) 26 27 // This test enforces that the User round trips. 28 func TestUserJson(t *testing.T) { 29 // Time is converted differently by the JSON functions depending on the OS, therefore a string comparison cannot 30 // be made without additional modifications. 31 ctx := sql.NewEmptyContext() 32 testUser := &User{ 33 User: "tester", 34 Host: "localhost", 35 PrivilegeSet: NewPrivilegeSet(), 36 Plugin: "mysql_native_password", 37 Password: "*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19", 38 PasswordLastChanged: time.Unix(184301, 0), 39 Locked: false, 40 Attributes: nil, 41 IsRole: false, 42 } 43 testUser.PrivilegeSet.AddGlobalStatic(sql.PrivilegeType_Super) 44 testUser.PrivilegeSet.AddDatabase("some_db", sql.PrivilegeType_Select, sql.PrivilegeType_Insert) 45 testUser.PrivilegeSet.AddTable("other_db", "some_tbl", sql.PrivilegeType_Update, sql.PrivilegeType_Delete) 46 testUser.PrivilegeSet.AddColumn("some_db", "other_tbl", "some_col", sql.PrivilegeType_Create, sql.PrivilegeType_Drop) 47 jsonStr, err := testUser.ToJson(ctx) 48 require.NoError(t, err) 49 newUser, err := (User{}).FromJson(ctx, jsonStr) 50 require.NoError(t, err) 51 require.True(t, UserEquals(testUser, newUser)) 52 53 testSlice := []*User{testUser} 54 jsonData, err := json.Marshal(testSlice) 55 require.NoError(t, err) 56 var newSlice []*User 57 err = json.Unmarshal(jsonData, &newSlice) 58 require.NoError(t, err) 59 require.Len(t, newSlice, len(testSlice)) 60 for i := range testSlice { 61 require.True(t, UserEquals(testSlice[i], newSlice[i])) 62 } 63 }