6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 18:51:03 +00:00
prologic-saltyim/client_test.go
James Mills 9c5fec5caf Fix bug when endpoint is invalid or not configured proeprly and fails lookup (#171)
Fixes #169

This now behaves like this:

```
/Users/prologic/Projects/saltyim/saltyim # ./salty-chat write prologic@mills.io test
WARN[0000] error looking up user endpoint                error="error looking up user testing123@shortcircuit.net.au: non-2xx response received: 404 Not Found"
error initializing client: unable to find your endpoint for testing123@shortcircuit.net.au
/Users/prologic/Projects/saltyim/saltyim #
```

Co-authored-by: James Mills <prologic@shortcircuit.net.au>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/171
Reviewed-by: m u t e f a l l <mutefall@noreply@mills.io>
2022-05-06 22:44:06 +00:00

60 lines
1.5 KiB
Go

package saltyim
import (
"crypto/sha256"
"fmt"
"net/url"
"testing"
"github.com/keys-pub/keys"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_InvalidEndpoint(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
me, err := ParseAddr("foo@example.com")
require.NoError(err)
assert.NotNil(me)
_, err = NewClient(me)
assert.Error(err)
}
func TestClient_Outbox(t *testing.T) {
test := require.New(t)
endpoint := &url.URL{Host: "example.com", Path: "/path", Scheme: "https"}
key := keys.GenerateEdX25519Key()
client := &Client{me: &Addr{endpoint: endpoint}, id: &Identity{key: key}}
outbox := client.Outbox()
test.True(endpoint.Path == "/path",
"endpoint.Path should not be modified after call to client.Outbox()")
test.False(*endpoint == *outbox,
"endpoint and outbox should not point to the same *url.URL")
expected := fmt.Sprintf("/%x", sha256.Sum256(key.Private()))
test.True(outbox.Path == expected, "expected %s but got %s", expected, outbox.Path)
}
func TestClient_Outbox_State(t *testing.T) {
req := require.New(t)
endpoint := &url.URL{Host: "example.com", Path: "/path", Scheme: "https"}
key := keys.GenerateEdX25519Key()
state := NewState()
var testIndex int64 = 100
state.SetIndex("testIndex", testIndex)
client := &Client{me: &Addr{endpoint: endpoint}, id: &Identity{key: key}, state: state}
outboxClient := client.OutboxClient(nil)
req.Equal(state, outboxClient.state)
req.Equal(testIndex, outboxClient.state.GetIndex("testIndex"))
}