Skip to content

Commit

Permalink
Added integration test for server initiated auth, refactored related …
Browse files Browse the repository at this point in the history
…test name
  • Loading branch information
sacOO7 committed Sep 2, 2024
1 parent 947de24 commit cfce4b2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ably/ably_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ func (rec *MessageRecorder) CheckIfReceived(action ably.ProtoAction, times int)
}
}
}
if times == 0 && times == counter {
return true
}
return false
}
}
Expand Down
3 changes: 3 additions & 0 deletions ably/proto_protocol_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ func (msg *protocolMessage) String() string {
case actionMessage:
return fmt.Sprintf("(action=%q, id=%q, messages=%v)", msg.Action,
msg.ConnectionID, msg.Messages)
case actionAuth:
return fmt.Sprintf("(action=%q, id=%q, auth=%v)", msg.Action,
msg.ConnectionID, msg.Auth)
default:
return fmt.Sprintf("%#v", msg)
}
Expand Down
53 changes: 52 additions & 1 deletion ably/realtime_conn_spec_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ func TestRealtimeConn_RTN15h3_Success(t *testing.T) {
ablytest.Instantly.NoRecv(t, nil, stateChanges, t.Fatalf)
}

func TestRealtimeConn_RTN15h_Integration_ClientInitiatedAuth(t *testing.T) {
func TestRealtimeConn_RTN22a_RTN15h2_Integration_ServerInitiatedAuth(t *testing.T) {
t.Parallel()
app, restClient := ablytest.NewREST()
defer safeclose(t, app)
Expand Down Expand Up @@ -1833,6 +1833,57 @@ func TestRealtimeConn_RTN15h_Integration_ClientInitiatedAuth(t *testing.T) {
assert.ElementsMatch(t, authCallbackTokens, tokens)
}

func TestRealtimeConn_RTN22_RTC8_Integration_ServerInitiatedAuth(t *testing.T) {
app, restClient := ablytest.NewREST()
defer safeclose(t, app)

recorder := NewMessageRecorder()
authCallbackTokens := []string{}

// Server sends AUTH message 30 seconds before token expiry.
// So sending client token with expiry of 33 seconds, server will send AUTH msg after 3 seconds.
authCallback := func(ctx context.Context, tp ably.TokenParams) (ably.Tokener, error) {
tokenExpiry := 33000
token, err := restClient.Auth.RequestToken(context.Background(), &ably.TokenParams{TTL: int64(tokenExpiry)})
authCallbackTokens = append(authCallbackTokens, token.Token)
return token, err
}

realtime, err := ably.NewRealtime(
ably.WithAutoConnect(false),
ably.WithDial(recorder.Dial),
ably.WithUseBinaryProtocol(false),
ably.WithEnvironment(ablytest.Environment),
ably.WithAuthCallback(authCallback))

assert.NoError(t, err)
defer realtime.Close()

err = ablytest.Wait(ablytest.ConnWaiter(realtime, realtime.Connect, ably.ConnectionEventConnected), nil)
assert.NoError(t, err)

for i := 0; i < 3; i++ {
// auth msg sent by ably server every 3 seconds, so connection is updated by client
err = ablytest.Wait(ablytest.ConnWaiter(realtime, nil, ably.ConnectionEventUpdate), nil)
assert.NoError(t, err)
assert.Equal(t, ably.ConnectionStateConnected, realtime.Connection.State())
assert.True(t, ablytest.Instantly.IsTrue(recorder.CheckIfReceived(ably.ActionAuth, i+1)))
}
assert.True(t, ablytest.Instantly.IsTrue(recorder.CheckIfReceived(ably.ActionDisconnected, 0)))

// Only one dial attempt
tokens := []string{}
assert.Len(t, recorder.URLs(), 1)
for _, url := range recorder.URLs() {
tokens = append(tokens, url.Query().Get("access_token"))
}
assert.Len(t, tokens, 1)

assert.Len(t, authCallbackTokens, 4)
assert.Equal(t, tokens[0], authCallbackTokens[0])
assertUnique(t, authCallbackTokens)
}

func TestRealtimeConn_RTN15i_OnErrorWhenConnected(t *testing.T) {

in := make(chan *ably.ProtocolMessage, 1)
Expand Down

0 comments on commit cfce4b2

Please sign in to comment.