databag/net/server/internal/ucAttachApp_test.go

104 lines
2.9 KiB
Go
Raw Normal View History

2022-01-17 07:00:08 +00:00
package databag
import (
"testing"
"encoding/json"
"time"
2022-01-19 20:07:57 +00:00
"github.com/gorilla/websocket"
2022-01-18 06:56:00 +00:00
"github.com/stretchr/testify/assert"
2022-01-17 07:00:08 +00:00
)
func TestAttachAccount(t *testing.T) {
2022-01-18 06:56:00 +00:00
2022-01-19 19:00:20 +00:00
// get account token
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
SetBasicAuth(r, "admin:pass")
AddNodeAccount(w, r)
var account string
2022-01-20 04:25:20 +00:00
assert.NoError(t, ReadResponse(w, &account))
2022-01-19 19:00:20 +00:00
// set account profile
r, w, _ = NewRequest("GET", "/account/profile", nil)
SetBearerAuth(r, account);
SetCredentials(r, "attachapp:pass")
AddAccount(w, r)
2022-01-22 19:04:29 +00:00
var profile Profile
assert.NoError(t, ReadResponse(w, &profile))
2022-01-19 19:00:20 +00:00
2022-01-18 06:56:00 +00:00
// acquire new token for attaching app
2022-01-19 19:00:20 +00:00
r, w, _ = NewRequest("POST", "/account/apps", nil)
SetBasicAuth(r, "attachapp:pass");
2022-01-18 06:56:00 +00:00
AddAccountApp(w, r);
var token string
assert.NoError(t, ReadResponse(w, &token))
// attach app with token
app := AppData{
Name: "Appy",
Description: "A test app",
Url: "http://app.example.com",
};
r, w, _ = NewRequest("PUT", "/account/apps", &app)
SetBearerAuth(r, token)
SetAccountApp(w, r)
var access string
assert.NoError(t, ReadResponse(w, &access))
// autorize app
r, w, _ = NewRequest("PUT", "/authorize", "aabbccdd")
2022-01-22 19:40:20 +00:00
SetBearerAuth(r, profile.Guid + "." + access)
Authorize(w, r);
var message DataMessage
assert.NoError(t, ReadResponse(w, &message))
2022-01-18 06:56:00 +00:00
// validate message
var auth Authenticate
2022-01-20 07:45:53 +00:00
guid, msgType, ts, err := ReadDataMessage(&message, &auth)
if err != nil {
PrintMsg(err)
}
cur := time.Now().Unix()
2022-01-20 07:45:53 +00:00
assert.GreaterOrEqual(t, cur, ts)
assert.Less(t, cur - 60, ts)
assert.Equal(t, "aabbccdd", auth.Token)
assert.Equal(t, msgType, APP_MSGAUTHENTICATE)
2022-01-19 19:00:20 +00:00
// app connects websocket
2022-01-19 20:07:57 +00:00
ws := getTestWebsocket()
2022-01-22 19:40:20 +00:00
announce := Announce{ AppToken: profile.Guid + "." + access }
2022-01-19 19:00:20 +00:00
msg, _ := json.Marshal(&announce)
ws.WriteMessage(websocket.TextMessage, msg)
2022-01-23 04:37:14 +00:00
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
2022-01-19 19:00:20 +00:00
_, msg, _ = ws.ReadMessage()
var revision Revision
assert.NoError(t, json.Unmarshal(msg, &revision))
profileRevision := revision.Profile
2022-01-18 06:56:00 +00:00
// set profile
2022-01-19 08:03:46 +00:00
profileData := ProfileData{
Name: "Namer",
Location: "San Francisco",
Description: "databaggerr",
};
r, w, _ = NewRequest("PUT", "/profile/data", &profileData)
2022-01-22 19:04:29 +00:00
SetBearerAuth(r, profile.Guid + "." + access)
2022-01-19 08:03:46 +00:00
SetProfile(w, r)
assert.NoError(t, ReadResponse(w, nil))
// get profile
r, w, _ = NewRequest("GET", "/profile", nil)
2022-01-22 19:04:29 +00:00
SetBearerAuth(r, profile.Guid + "." + access)
2022-01-19 08:03:46 +00:00
GetProfile(w, r)
assert.NoError(t, ReadResponse(w, &profile))
assert.Equal(t, guid, profile.Guid)
2022-01-19 19:00:20 +00:00
assert.Equal(t, "attachapp", profile.Handle)
2022-01-19 08:03:46 +00:00
assert.Equal(t, "Namer", profile.Name)
2022-01-19 19:00:20 +00:00
// profile revision incremented
2022-01-23 04:37:14 +00:00
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
2022-01-19 19:00:20 +00:00
_, msg, _ = ws.ReadMessage()
assert.NoError(t, json.Unmarshal(msg, &revision))
assert.NotEqual(t, profileRevision, revision.Profile)
2022-01-17 07:00:08 +00:00
}