Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from MattSFT/master
Browse files Browse the repository at this point in the history
Fix clientActivityId being null. It is not guaranteed to be present for all channels.
  • Loading branch information
maxpert authored Sep 13, 2017
2 parents 0b6d870 + 2906ea4 commit da9fa2e
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/adapter-middleware.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TextMiddleware extends BaseMiddleware
user.activity = activity

if activity.type == 'message'
return new TextMessage(user, activity.text, activity.sourceEvent.clientActivityId)
return new TextMessage(user, activity.text, activity.sourceEvent?.clientActivityId || '')

return new Message(user)

Expand Down Expand Up @@ -56,4 +56,4 @@ module.exports = {

BaseMiddleware
TextMiddleware
}
}
182 changes: 182 additions & 0 deletions test/adapter-middleware.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
chai = require 'chai'
expect = chai.expect
{ TextMessage, Message, User } = require 'hubot'
MockRobot = require './mock-robot'
{ BaseMiddleware, TextMiddleware, middlewareFor } = require '../src/adapter-middleware'

describe 'middlewareFor', ->
it 'should return Middleware for null', ->
middleware = middlewareFor(null)
expect(middleware).to.be.not.null

it 'should return Middleware for any string', ->
middleware = middlewareFor("null")
expect(middleware).to.be.not.null

it 'should return Middleware for proper channel', ->
middleware = middlewareFor("msteams")
expect(middleware).to.be.not.null


describe 'BaseMiddleware', ->
describe 'toReceivable', ->
robot = null
event = null
beforeEach ->
robot = new MockRobot
event =
type: 'message'
text: 'Bot do something and tell User about it'
agent: 'tests'
source: '*'
address:
conversation:
id: "conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"

it 'should throw', ->
middleware = new BaseMiddleware(robot)
expect(() ->
middleware.toReceivable(event)
).to.throw()

describe 'toSendable', ->
robot = null
message = null
context = null
beforeEach ->
robot = new MockRobot
context =
user:
id: 'user-id'
name: 'user-name'
activity:
type: 'message'
text: 'Bot do something and tell User about it'
agent: 'tests'
source: '*'
address:
conversation:
id: "conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"
message = "message"

it 'should throw', ->
middleware = new BaseMiddleware(robot)
expect(() ->
middleware.toSendable(context, message)
).to.throw()

describe 'TextMiddleware', ->
describe 'toReceivable', ->
robot = null
event = null
beforeEach ->
robot = new MockRobot
event =
type: 'message'
text: 'Bot do something and tell User about it'
agent: 'tests'
source: '*'
address:
conversation:
id: "conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"

it 'return generic message when appropriate type is not found', ->
# Setup
event.type = 'typing'
middleware = new TextMiddleware(robot)

# Action
receivable = null
expect(() ->
receivable = middleware.toReceivable(event)
).to.not.throw()

# Assert
expect(receivable).to.be.not.null

it 'return message when type is message', ->
# Setup
middleware = new TextMiddleware(robot)

# Action
receivable = null
expect(() ->
receivable = middleware.toReceivable(event)
).to.not.throw()

# Assert
expect(receivable).to.be.not.null

describe 'toSendable', ->
robot = null
message = null
context = null
beforeEach ->
robot = new MockRobot
context =
user:
id: 'user-id'
name: 'user-name'
activity:
type: 'message'
text: 'Bot do something and tell User about it'
agent: 'tests'
source: '*'
address:
conversation:
id: "conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"
message = "message"

it 'should create message object for string messages', ->
# Setup
middleware = new TextMiddleware(robot)

# Action
sendable = null
expect(() ->
sendable = middleware.toSendable(context, message)
).to.not.throw()

# Verify
expected = {
type: 'message'
text: message
address: context.user.activity.address
}
expect(sendable).to.deep.equal(expected)

it 'should not alter non-string messages', ->
# Setup
message =
type: "some message type"
middleware = new TextMiddleware(robot)

# Action
sendable = null
expect(() ->
sendable = middleware.toSendable(context, message)
).to.not.throw()

# Verify
expected = message
expect(sendable).to.deep.equal(expected)
14 changes: 7 additions & 7 deletions test/msteams-middleware.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe 'MicrosoftTeamsMiddleware', ->
id: "19:conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"
user:
id: "user-id"
name: "user-name"

it 'should allow messages without tenant id when tenant filter is empty', ->
# Setup
Expand Down Expand Up @@ -171,7 +171,7 @@ describe 'MicrosoftTeamsMiddleware', ->

it 'should prepend bot name in 1:1 chats', ->
# Setup
event.address.conversation.id = event.user.id
event.address.conversation.id = event.address.user.id
event.text = 'do something <at>Bot</at> and tell <at>User</at> about it'
teamsMiddleware = new MicrosoftTeamsMiddleware(robot)

Expand Down Expand Up @@ -221,9 +221,9 @@ describe 'MicrosoftTeamsMiddleware', ->
id: "19:conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"
user:
id: "user-id"
name: "user-name"
message = "message"

it 'should create message object for string messages', ->
Expand Down

0 comments on commit da9fa2e

Please sign in to comment.