func NewValidStone() *Stone { var meta = map[string]interface{}{ "id": util.NewID(), "type": "some_stone", "created_at": time.Now().Unix(), } sh, _ := Create(meta, util.ReadFromFixtures("tests/fixtures/rsa_priv_1.txt")) return sh }
// TestValidateWithMap tests that a map representing a stone can be passed to Validate() func TestValidateWithMap(t *testing.T) { s := map[string]interface{}{ "meta": map[string]interface{}{ "id": util.NewID(), "type": "coupon", "created_at": time.Now().Unix(), }, } err := Validate(s) assert.Nil(t, err) }
// TestInvalidPrivateKey tests that an invalid private key returns an error func TestInvalidPrivateKey(t *testing.T) { var issuerPrivateKey = util.ReadFromFixtures("tests/fixtures/rsa_invalid_1.txt") stoneID := util.NewID() var meta = map[string]interface{}{ "id": stoneID, "type": "currency", "created_at": time.Now().Unix(), } _, err := Create(meta, issuerPrivateKey) assert.NotNil(t, err) assert.Equal(t, err.Error(), `Private Key Error: unsupported key type "KEY"`) }
// TestTokenToBlockSuccessfully tests that a JWS token is successfully decoded to a block func TestTokenToBlockSuccessfully(t *testing.T) { var meta = map[string]interface{}{ "id": util.NewID(), "type": "currency", "created_at": time.Now().Unix(), } sh, err := Create(meta, util.ReadFromFixtures("tests/fixtures/rsa_priv_1.txt")) assert.Nil(t, err) block, err := TokenToBlock(sh.Signatures["meta"].(string), "meta") assert.Nil(t, err) assert.Equal(t, meta["id"], block["id"]) }
// TestEncodeSuccessfully tests that a stone was encoded successfully func TestEncodeSuccessfully(t *testing.T) { var meta = map[string]interface{}{ "id": util.NewID(), "type": "currency", "created_at": time.Now().Unix(), } sh, err := Create(meta, util.ReadFromFixtures("tests/fixtures/rsa_priv_1.txt")) assert.Nil(t, err) enc, _ := util.MapToJSON(sh.Signatures) expectedEncodeVal := crypto.ToBase64Raw([]byte(enc)) assert.Equal(t, sh.Encode(), expectedEncodeVal) }
// TestCreateAStone create a valid, error free stone func TestCreateAStone(t *testing.T) { stoneID := util.NewID() var meta = map[string]interface{}{ "id": stoneID, "type": "currency", "created_at": time.Now().Unix(), } sh, err := Create(meta, util.ReadFromFixtures("tests/fixtures/rsa_priv_1.txt")) assert.Nil(t, err) assert.Equal(t, sh.Meta["id"], stoneID) assert.NotEmpty(t, sh.Signatures["meta"]) }
// TestHasEmbedsTrue tests that a stone has it's embeds // block set func TestHasEmbedsTrue(t *testing.T) { sh := NewValidStone() embeds := map[string]interface{}{ "ref_id": sh.Meta["id"], "data": []interface{}{ map[string]interface{}{ "meta": map[string]interface{}{ "id": util.NewID(), "type": "coupon", "created_at": time.Now().Unix(), }, }, }, } assert.Equal(t, sh.HasEmbeds(), false) err := sh.AddEmbed(embeds, util.ReadFromFixtures("tests/fixtures/rsa_priv_1.txt")) assert.Nil(t, err) assert.Equal(t, sh.HasEmbeds(), true) }
// TestIgnoreChildEmbedsOfEmbeds tests that child embeds are not validated func TestIgnoreChildEmbedsOfEmbeds(t *testing.T) { d := map[string]interface{}{ "ref_id": "xxx", "data": []interface{}{ map[string]interface{}{ "meta": map[string]interface{}{ "id": util.NewID(), "type": "coupon", "created_at": time.Now().Unix(), }, "embeds": map[string]interface{}{ "ref_id": "xxx", "data": "*invalid_type*", }, }, }, } err := ValidateEmbedsBlock(d, "xxx") assert.Nil(t, err) childEmbed := d["data"].([]interface{})[0].(map[string]interface{}) assert.NotNil(t, childEmbed["embeds"]) assert.NotNil(t, childEmbed["embeds"].(map[string]interface{})["data"]) }