token_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 Yunion
  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. package policy
  15. import (
  16. "testing"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. )
  20. func TestSerialize(t *testing.T) {
  21. type SEmbed struct {
  22. UserCred mcclient.TokenCredential
  23. }
  24. token := &mcclient.SSimpleToken{}
  25. token.User = "Test"
  26. emb := SEmbed{}
  27. emb.UserCred = token
  28. jsonEmb := jsonutils.Marshal(&emb)
  29. t.Logf("json: %s", jsonEmb)
  30. nemb := SEmbed{}
  31. err := jsonEmb.Unmarshal(&nemb)
  32. if err != nil {
  33. t.Errorf("fail to unmarshal: %s", err)
  34. } else {
  35. jsonEmb2 := jsonutils.Marshal(&nemb)
  36. t.Logf("unmarshal: %s", jsonEmb2)
  37. }
  38. }
  39. func TestSerialize2(t *testing.T) {
  40. type SEmbed struct {
  41. UserCred mcclient.TokenCredential
  42. }
  43. type SUnEmbed struct {
  44. UserCred struct {
  45. TokenCredential mcclient.TokenCredential
  46. }
  47. }
  48. token := &SPolicyTokenCredential{
  49. &mcclient.SSimpleToken{
  50. User: "Test",
  51. },
  52. }
  53. emb := SEmbed{}
  54. emb.UserCred = token
  55. jsonEmb := jsonutils.Marshal(&emb)
  56. t.Logf("json: %s", jsonEmb)
  57. nemb := SUnEmbed{}
  58. err := jsonEmb.Unmarshal(&nemb)
  59. if err != nil {
  60. t.Errorf("fail to unmarshal: %s", err)
  61. } else {
  62. jsonEmb2 := jsonutils.Marshal(&nemb)
  63. t.Logf("unmarshal: %s", jsonEmb2)
  64. }
  65. }