github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/bridge_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	. "github.com/holochain/holochain-proto/hash"
     7  	. "github.com/smartystreets/goconvey/convey"
     8  	"testing"
     9  )
    10  
    11  func TestBridgeCall(t *testing.T) {
    12  	d, _, h := PrepareTestChain("test")
    13  	defer CleanupTestChain(h, d)
    14  
    15  	token := "bogus token"
    16  	var err error
    17  	Convey("it should fail calls to functions when there's no brided", t, func() {
    18  		_, err = h.BridgeCall("zySampleZome", "testStrFn1", "arg1 arg2", token)
    19  		So(err.Error(), ShouldEqual, "no active bridge")
    20  	})
    21  
    22  	fakeFromApp, _ := NewHash("QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx")
    23  	Convey("it should call the bridgeGenesis function when bridging on the to side", t, func() {
    24  		ShouldLog(h.nucleus.alog, func() {
    25  			token, err = h.AddBridgeAsCallee(fakeFromApp, "app data")
    26  			So(err, ShouldBeNil)
    27  		}, `bridge genesis to-- other side is:`+fakeFromApp.String()+` bridging data:app data`)
    28  		c := Capability{Token: token, db: h.bridgeDB}
    29  		bridgeSpecStr, err := c.Validate(nil)
    30  		So(err, ShouldBeNil)
    31  		So(bridgeSpecStr, ShouldEqual, `{"jsSampleZome":{"getProperty":true},"zySampleZome":{"testStrFn1":true}}`)
    32  	})
    33  
    34  	fakeToApp, _ := NewHash("QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHy")
    35  	url := "http://localhost:31415"
    36  	Convey("it should fail on an unknown bridging zome", t, func() {
    37  		err := h.AddBridgeAsCaller("fooZome", fakeToApp, "fakeAppName", token, url, "app data")
    38  		So(err.Error(), ShouldEqual, "error getting bridging zome: unknown zome: fooZome")
    39  	})
    40  
    41  	Convey("it should call the bridgeGenesis function when bridging on the from side", t, func() {
    42  
    43  		bridgeZome := h.nucleus.dna.Zomes[0].Name
    44  		ShouldLog(h.nucleus.alog, func() {
    45  			url := "http://localhost:31415"
    46  			err := h.AddBridgeAsCaller(bridgeZome, fakeToApp, "fakeAppName", token, url, "app data")
    47  			So(err, ShouldBeNil)
    48  		}, `bridge genesis from-- other side is:`+fakeToApp.String()+` bridging data:app data`)
    49  	})
    50  
    51  	Convey("it should call the bridged function", t, func() {
    52  		var result interface{}
    53  		result, err = h.BridgeCall("zySampleZome", "testStrFn1", "arg1 arg2", token)
    54  		So(err, ShouldBeNil)
    55  		So(result.(string), ShouldEqual, "result: arg1 arg2")
    56  	})
    57  
    58  	Convey("it should fail calls to functions not included in the bridge", t, func() {
    59  		_, err = h.BridgeCall("zySampleZome", "testStrFn2", "arg1 arg2", token)
    60  		So(err, ShouldNotBeNil)
    61  		So(err.Error(), ShouldEqual, "function not bridged")
    62  	})
    63  
    64  }
    65  
    66  func TestBridgeSpec(t *testing.T) {
    67  	spec := BridgeSpec{
    68  		"bridgedZome": {"bridgedFunc": true},
    69  	}
    70  	Convey("it should fail functions not in the spec", t, func() {
    71  		So(checkBridgeSpec(spec, "someZome", "someFunc"), ShouldBeFalse)
    72  		So(checkBridgeSpec(spec, "bridgedZome", "someFunc"), ShouldBeFalse)
    73  		So(checkBridgeSpec(spec, "someZome", "bridgedFunc"), ShouldBeFalse)
    74  	})
    75  	Convey("it should not fail functions in the spec", t, func() {
    76  		So(checkBridgeSpec(spec, "bridgedZome", "bridgedFunc"), ShouldBeTrue)
    77  	})
    78  }
    79  
    80  func TestBridgeSpecMake(t *testing.T) {
    81  	d, _, h := PrepareTestChain("test")
    82  	defer CleanupTestChain(h, d)
    83  
    84  	Convey("it should make spec from the dna", t, func() {
    85  		spec := h.makeBridgeSpec()
    86  		bridgeSpecB, _ := json.Marshal(spec)
    87  
    88  		So(fmt.Sprintf("%s", string(bridgeSpecB)), ShouldEqual, `{"jsSampleZome":{"getProperty":true},"zySampleZome":{"testStrFn1":true}}`)
    89  	})
    90  }
    91  
    92  func TestBridgeStore(t *testing.T) {
    93  	d, _, h := SetupTestChain("test")
    94  	defer CleanupTestChain(h, d)
    95  
    96  	hash, _ := NewHash("QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw")
    97  	token := "some token"
    98  	url := "http://localhost:31415"
    99  	Convey("it should add ba token to the bridged apps list", t, func() {
   100  		err := h.AddBridgeAsCaller("jsSampleZome", hash, "fakeAppName", token, url, "")
   101  		So(err, ShouldBeNil)
   102  		t, u, err := h.GetBridgeToken(hash)
   103  		So(err, ShouldBeNil)
   104  		So(t, ShouldEqual, token)
   105  		So(u, ShouldEqual, url)
   106  	})
   107  }
   108  
   109  func TestBridgeGetBridges(t *testing.T) {
   110  	d, _, h := SetupTestChain("test")
   111  	defer CleanupTestChain(h, d)
   112  
   113  	Convey("it should return an empty list", t, func() {
   114  		bridges, err := h.GetBridges()
   115  		So(err, ShouldBeNil)
   116  		So(len(bridges), ShouldEqual, 0)
   117  	})
   118  
   119  	fakeToApp, _ := NewHash("QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw")
   120  	token := "some token"
   121  	url := "http://localhost:31415"
   122  	err := h.AddBridgeAsCaller("jsSampleZome", fakeToApp, "fakeAppName", token, url, "")
   123  	if err != nil {
   124  		panic(err)
   125  	}
   126  
   127  	fakeFromApp, _ := NewHash("QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHx")
   128  	_, err = h.AddBridgeAsCallee(fakeFromApp, "app data")
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	Convey("it should return the bridged apps", t, func() {
   134  		bridges, err := h.GetBridges()
   135  		So(err, ShouldBeNil)
   136  		So(bridges[0].Side, ShouldEqual, BridgeCaller)
   137  		So(bridges[0].CalleeApp.String(), ShouldEqual, "QmVGtdTZdTFaLsaj2RwdVG8jcjNNcp1DE914DKZ2kHmXHw")
   138  		So(bridges[0].CalleeName, ShouldEqual, "fakeAppName")
   139  		So(bridges[1].Side, ShouldEqual, BridgeCallee)
   140  		So(bridges[1].Token, ShouldNotEqual, 0)
   141  	})
   142  }