github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/user_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	"time"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/yaml.v2"
    12  
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type UserSerializationSuite struct {
    17  	testing.BaseSuite
    18  }
    19  
    20  var _ = gc.Suite(&UserSerializationSuite{})
    21  
    22  func (*UserSerializationSuite) TestNil(c *gc.C) {
    23  	_, err := importUsers(nil)
    24  	c.Check(err, gc.ErrorMatches, "users version schema check failed: .*")
    25  }
    26  
    27  func (*UserSerializationSuite) TestMissingVersion(c *gc.C) {
    28  	_, err := importUsers(map[string]interface{}{
    29  		"users": []interface{}{},
    30  	})
    31  	c.Check(err.Error(), gc.Equals, "users version schema check failed: version: expected int, got nothing")
    32  }
    33  
    34  func (*UserSerializationSuite) TestMissingUsers(c *gc.C) {
    35  	_, err := importUsers(map[string]interface{}{
    36  		"version": 1,
    37  	})
    38  	c.Check(err.Error(), gc.Equals, "users version schema check failed: users: expected list, got nothing")
    39  }
    40  
    41  func (*UserSerializationSuite) TestNonIntVersion(c *gc.C) {
    42  	_, err := importUsers(map[string]interface{}{
    43  		"version": "hello",
    44  		"users":   []interface{}{},
    45  	})
    46  	c.Check(err.Error(), gc.Equals, `users version schema check failed: version: expected int, got string("hello")`)
    47  }
    48  
    49  func (*UserSerializationSuite) TestUnknownVersion(c *gc.C) {
    50  	_, err := importUsers(map[string]interface{}{
    51  		"version": 42,
    52  		"users":   []interface{}{},
    53  	})
    54  	c.Check(err.Error(), gc.Equals, `version 42 not valid`)
    55  }
    56  
    57  func (*UserSerializationSuite) TestParsingSerializedData(c *gc.C) {
    58  	lastConn := time.Date(2016, 1, 15, 12, 0, 0, 0, time.UTC)
    59  	initial := users{
    60  		Version: 1,
    61  		Users_: []*user{
    62  			&user{
    63  				Name_:           "admin",
    64  				CreatedBy_:      "admin",
    65  				DateCreated_:    time.Date(2015, 10, 9, 12, 34, 56, 0, time.UTC),
    66  				LastConnection_: &lastConn,
    67  			},
    68  			&user{
    69  				Name_:        "read-only",
    70  				DisplayName_: "A read only user",
    71  				CreatedBy_:   "admin",
    72  				DateCreated_: time.Date(2015, 10, 9, 12, 34, 56, 0, time.UTC),
    73  				Access_:      "read",
    74  			},
    75  		},
    76  	}
    77  
    78  	bytes, err := yaml.Marshal(initial)
    79  	c.Assert(err, jc.ErrorIsNil)
    80  
    81  	c.Logf("%s", bytes)
    82  
    83  	var source map[string]interface{}
    84  	err = yaml.Unmarshal(bytes, &source)
    85  	c.Assert(err, jc.ErrorIsNil)
    86  
    87  	users, err := importUsers(source)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  
    90  	c.Assert(users, jc.DeepEquals, initial.Users_)
    91  }