Official cwtch.im peer and server implementations. https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile_store_test.go 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package storage
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "os"
  5. "testing"
  6. "time"
  7. )
  8. const testProfileName = "Alice"
  9. const testKey = "key"
  10. const testVal = "value"
  11. const testInitialMessage = "howdy"
  12. const testMessage = "Hello from storage"
  13. func TestProfileStoreWriteRead(t *testing.T) {
  14. os.RemoveAll(testingDir)
  15. eventBus := event.NewEventManager()
  16. profile := NewProfile(testProfileName)
  17. ps1 := NewProfileWriterStore(eventBus, testingDir, password, profile)
  18. eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{event.Key: testKey, event.Data: testVal}))
  19. time.Sleep(1 * time.Second)
  20. groupid, invite, err := profile.StartGroup("2c3kmoobnyghj2zw6pwv7d57yzld753auo3ugauezzpvfak3ahc4bdyd")
  21. if err != nil {
  22. t.Errorf("Creating group: %v\n", err)
  23. }
  24. if err != nil {
  25. t.Errorf("Creating group invite: %v\n", err)
  26. }
  27. eventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().Format(time.RFC3339Nano), event.RemotePeer: ps1.GetProfileCopy(true).Onion, event.GroupInvite: string(invite)}))
  28. time.Sleep(1 * time.Second)
  29. eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{
  30. event.GroupID: groupid,
  31. event.TimestampSent: time.Now().Format(time.RFC3339Nano),
  32. event.TimestampReceived: time.Now().Format(time.RFC3339Nano),
  33. event.RemotePeer: ps1.GetProfileCopy(true).Onion,
  34. event.Data: testMessage,
  35. }))
  36. time.Sleep(1 * time.Second)
  37. ps1.Shutdown()
  38. ps2 := NewProfileWriterStore(eventBus, testingDir, password, nil)
  39. err = ps2.Load()
  40. if err != nil {
  41. t.Errorf("Error createing profileStore: %v\n", err)
  42. }
  43. profile = ps2.GetProfileCopy(true)
  44. if profile.Name != testProfileName {
  45. t.Errorf("Profile name from loaded profile incorrect. Expected: '%v' Actual: '%v'\n", testProfileName, profile.Name)
  46. }
  47. v, _ := profile.GetAttribute(testKey)
  48. if v != testVal {
  49. t.Errorf("Profile attribute '%v' inccorect. Expected: '%v' Actual: '%v'\n", testKey, testVal, v)
  50. }
  51. group2 := ps2.GetProfileCopy(true).Groups[groupid]
  52. if group2 == nil {
  53. t.Errorf("Group not loaded\n")
  54. }
  55. }