Added tests for adapter.coffee, adapter-middleware, msteams-middleware, and hubot-response-cards

This commit is contained in:
Melanie Mendoza 2018-08-14 17:27:29 -07:00
Родитель 9213604269
Коммит 3da9968a72
6 изменённых файлов: 740 добавлений и 301 удалений

Просмотреть файл

@ -87,8 +87,8 @@ class BotFrameworkAdapter extends Adapter
# Return an error to the user if the message channel doesn't support authorization
# and authorization is enabled
if @enableAuth == 'true'
@robot.logger.info "#{LogPrefix} Authorization isn\'t supported for the channel"
text = "Authorization isn't supported for the channel"
@robot.logger.info "#{LogPrefix} Authorization isn\'t supported for the channel error"
text = "Authorization isn't supported for this channel"
payload = middleware.constructErrorResponse(activity, text)
@sendPayload(@robot, payload)
return

Просмотреть файл

@ -62,10 +62,8 @@ maybeConstructMenuInputCard = (query) ->
'data': data
}
]
return card
# Initializes card structure
initializeAdaptiveCard = (query) ->
card = {
@ -212,8 +210,12 @@ getFollowUpButtons = (query, regex) ->
return actions
# Appends the card body of card2 to card1, skipping
# duplicate card body blocks, and returns card1
# duplicate card body blocks, and returns card1. In the
# case that both card bodies are undefined
appendCardBody = (card1, card2) ->
if card2.content.body is undefined
return card1
if card1.content.body is undefined
card1.content.body = card2.content.body
return card1
@ -232,6 +234,9 @@ appendCardBody = (card1, card2) ->
# Appends the card actions of card2 to those of card1, skipping
# actions which card1 already contains
appendCardActions = (card1, card2) ->
if card2.content.actions is undefined
return card1
if card1.content.actions is undefined
card1.content.actions = card2.content.actions
return card1
@ -246,7 +251,6 @@ appendCardActions = (card1, card2) ->
# if not in storedActions, add it
if not hasAction
card1.content.actions.push(newAction)
return card1
# Helper method to create a short version of the command by including only the

Просмотреть файл

@ -63,7 +63,6 @@ class MicrosoftTeamsMiddleware extends BaseMiddleware
teamsConnector.fetchMembers activity?.address?.serviceUrl, \
activity?.address?.conversation?.id, (err, chatMembers) =>
if err
console.log("YUP AN ERR")
return
# Return with unauthorized error as true if auth is enabled and the user who sent
@ -198,11 +197,9 @@ class MicrosoftTeamsMiddleware extends BaseMiddleware
query = event.value.hubotMessage
# Remove hubot from the beginning of the command if it's there
query = query.replace("hubot ", "")
console.log(query)
card = HubotResponseCards.maybeConstructMenuInputCard(query)
if card is null
console.log("CARD IS NULL")
return null
response =

Просмотреть файл

@ -189,3 +189,45 @@ describe 'TextMiddleware', ->
# Action and Assert
expect(middleware.supportsAuth()).to.be.false
describe 'constructErrorResponse', ->
it 'return generic message when appropriate type is not found', ->
# Setup
robot = new MockRobot
middleware = new TextMiddleware(robot)
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"
# Action
result = null
expect(() ->
result = middleware.constructErrorResponse(event, "an error message")
).to.not.throw()
# Assert
expect(result).to.eql {
type: 'message'
text: 'an error message'
address:
conversation:
id: "conversation-id"
bot:
id: "bot-id"
user:
id: "user-id"
name: "user-name"
}

Просмотреть файл

@ -62,7 +62,7 @@ describe 'Main Adapter', ->
'authorized_user@email.la': true
}
describe 'Test Authorization Support for Teams Channel', ->
describe 'Test Authorization Not Suppported Error', ->
robot = null
adapter = null
event = null
@ -71,9 +71,13 @@ describe 'Main Adapter', ->
process.env.BOTBUILDER_APP_ID = 'botbuilder-app-id'
process.env.BOTBUILDER_APP_PASSWORD = 'botbuilder-app-password'
process.env.HUBOT_TEAMS_ENABLE_AUTH = 'true'
robot = new MockRobot
adapter = BotFrameworkAdapter.use(robot)
robot.adapter = adapter
adapter.connector.send = (payload, cb) ->
robot.brain.set("payload", payload)
event =
type: 'message'
text: '<at>Bot</at> do something <at>Bot</at> and tell <at>User</at> about it'
@ -118,31 +122,8 @@ describe 'Main Adapter', ->
).to.not.throw()
# Assert
result = robot.brain.get("event")
expect(result.text).to.eql "hubot return source authorization not supported error"
it 'should work when authorization is enabled and message is from Teams', ->
# Setup
# Action
expect(() ->
adapter.handleActivity(event)
).to.not.throw()
# Assert
result = robot.brain.get("event")
it 'should work when message is from invoke', ->
# Setup
event.type = 'invoke'
event.value =
hubotMessage: 'hubot do something'
delete event.text
# Action
expect(() ->
adapter.sendTextToHubot(event)
).to.not.throw()
# Assert
result = robot.brain.get("payload")
expect(result).to.be.a('Array')
expect(result.length).to.eql 1
expect(result[0].text).to.eql "Authorization isn't supported for this channel"

Просмотреть файл

@ -1,11 +1,18 @@
# Description:
# Tests for helper methods used to construct Adaptive Cards for specific hubot
# commands when used with the Botframework adapter
chai = require 'chai'
expect = chai.expect
HubotResponseCards = require '../src/hubot-response-cards'
describe 'MicrosoftTeamsMiddleware', ->
# Define any needed variables
describe 'HubotResponseCards', ->
describe 'maybeConstructResponseCard', ->
query = null
response = null
beforeEach ->
query = 'hubot gho create team team-name'
response = {
type: 'message',
text: 'The team: `team-name` was successfully created',
@ -16,6 +23,7 @@ describe 'MicrosoftTeamsMiddleware', ->
id: 'user-id',
name: 'user-name',
aadObjectId: 'user-aad-id'
userPrincipalName: 'user-UPN'
},
conversation: {
conversationType: 'conversation-type',
@ -29,32 +37,25 @@ describe 'MicrosoftTeamsMiddleware', ->
}
}
it 'should not construct card', ->
it 'should not construct response card for the query', ->
# Setup
query = 'hubot ping'
# Action and Assert
# Action
card = null
expect(() ->
card = HubotResponseCards.maybeConstructCard(response, query)
expect(card).to.be.null
card = HubotResponseCards.maybeConstructResponseCard(response, query)
).to.not.throw()
it 'should construct card', ->
# Assert
expect(card).to.be.null
it 'should construct response card for the query', ->
# Setup
query = 'hubot gho create team team-name'
followUp1 = 'gho add (members|repos) <members|repos> to team <team name>'
followUp2 = 'gho list (teams|repos|members)'
followUp3 = 'gho delete team <team name>'
# Action
card = null
expect(() ->
card = HubotResponseCards.maybeConstructCard(response, query)
expect(card).to.be.not.null
).to.not.throw()
# Assert
expected = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
@ -66,7 +67,7 @@ describe 'MicrosoftTeamsMiddleware', ->
'text': "#{query}"
'speak': "<s>#{query}</s>"
'weight': 'bolder'
'size': 'medium'
'size': 'large'
},
{
'type': 'TextBlock'
@ -76,15 +77,15 @@ describe 'MicrosoftTeamsMiddleware', ->
],
'actions': [
{
"title": "gho add "
"title": "gho add"
"type": "Action.ShowCard"
"card": {
"type": "AdaptiveCard"
"body": [
{
'type': 'TextBlock'
'text': "gho add "
'speak': "<s>gho add </s>"
'text': "gho add"
'speak': "<s>gho add</s>"
'weight': 'bolder'
'size': 'large'
},
@ -152,15 +153,15 @@ describe 'MicrosoftTeamsMiddleware', ->
}
},
{
"title": "gho list "
"title": "gho list"
"type": "Action.ShowCard"
"card": {
"type": "AdaptiveCard"
"body": [
{
'type': 'TextBlock'
'text': "gho list "
'speak': "<s>gho list </s>"
'text': "gho list"
'speak': "<s>gho list</s>"
'weight': 'bolder'
'size': 'large'
},
@ -204,15 +205,15 @@ describe 'MicrosoftTeamsMiddleware', ->
}
},
{
"title": "gho delete team "
"title": "gho delete team"
"type": "Action.ShowCard"
"card": {
"type": "AdaptiveCard"
"body": [
{
'type': 'TextBlock'
'text': "gho delete team "
'speak': "<s>gho delete team </s>"
'text': "gho delete team"
'speak': "<s>gho delete team</s>"
'weight': 'bolder'
'size': 'large'
},
@ -246,48 +247,462 @@ describe 'MicrosoftTeamsMiddleware', ->
]
}
}
# Action
card = null
expect(() ->
card = HubotResponseCards.maybeConstructResponseCard(response, query)
).to.not.throw()
# Assert
expect(card).to.eql(expected)
# # Test initializeAdaptiveCard
# it 'should initialize the adaptive card properly', ->
# # Setup
# text = "This should be the text for the title of the card"
describe 'maybeConstructMenuInputCard', ->
it 'should not construct menu input card for the query', ->
# Setup
query = 'ping'
# # Action
# card = initializeAdaptiveCard(text)
# Action
result = null
expect(() ->
result = HubotResponseCards.maybeConstructMenuInputCard(query)
)
# # Assert
# expected = {
# 'contentType': 'application/vnd.microsoft.card.adaptive'
# 'content': {
# "type": "AdaptiveCard"
# "version": "1.0"
# }
# 'body': [
# {
# 'type': 'TextBlock'
# 'text': "#{text}"
# 'speak': "<s>#{text}</s>"
# 'weight': 'bolder'
# 'size': 'medium'
# }
# ]
# }
# expect(card).to.equal(expected)
# Assert
expect(result).to.be.null
it 'should construct menu input card for the query', ->
# Setup
query = 'gho list (teams|repos|members)'
expected = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"body": [
{
'type': 'TextBlock'
'text': "gho list"
'speak': "<s>gho list</s>"
'weight': 'bolder'
'size': 'large'
},
{
'type': 'TextBlock'
'text': 'List what?'
'speak': "<s>List what?</s>"
},
{
"type": "Input.ChoiceSet"
"id": "gho list (teams|repos|members) - input0"
"style": "compact"
"value": "teams"
"choices": [
{
"title": "teams"
"value": "teams"
},
{
"title": "repos"
"value": "repos"
},
{
"title": "members"
"value": "members"
}
]
}
],
'actions': [
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
'queryPrefix': "gho list (teams|repos|members)"
"gho list (teams|repos|members) - query0": 'hubot gho list '
}
}
]
}
}
# # Test addTextBlock
# it 'should add a TextBlock', ->
# # Setup
# Action
result = null
expect(() ->
result = HubotResponseCards.maybeConstructMenuInputCard(query)
).to.not.throw()
# # Action
# Assert
expect(result).to.eql(expected)
# # Assert
describe 'appendCardBody', ->
card1 = null
card2 = null
expected = null
beforeEach ->
card1 = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"body": [
{
'type': 'TextBlock'
'text': "Card1"
'speak': "<s>Card1</s>"
'weight': 'bolder'
'size': 'large'
},
{
'type': 'Input.Text'
'id': "the-same-id"
'speak': "<s>the same text</s>"
'wrap': true
'style': 'text'
},
{
'type': 'TextBlock'
'text': "This is unique to 1"
'speak': "<s>This is unique to 1</s>"
},
{
"type": "Input.ChoiceSet"
"id": "a-selector-unique-to-card1-id"
"style": "compact"
"choices": [
{
"title": "Card 1 choice"
"value": "Card 1 choice"
},
{
"title": "Another card 1 choice"
"value": "Another card 1 choice"
}
]
"value": "Another card 1 choice"
}
]
}
}
card2 = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"body": [
{
'type': 'TextBlock'
'text': "Card2"
'speak': "<s>Card2</s>"
'weight': 'bolder'
'size': 'large'
},
{
'type': 'TextBlock'
'text': "This is unique to 2"
'speak': "<s>This is unique to 2</s>"
},
{
'type': 'Input.Text'
'id': "the-same-id"
'speak': "<s>the same text</s>"
'wrap': true
'style': 'text'
},
{
"type": "Input.ChoiceSet"
"id": "a-selector-unique-to-card2-id"
"style": "compact"
"choices": [
{
"title": "Card 2 choice"
"value": "Card 2 choice"
},
{
"title": "Another card 2 choice"
"value": "Another card 2 choice"
}
]
"value": "Another card 2 choice"
}
]
}
}
expected = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"body": [
{
'type': 'TextBlock'
'text': "Card1"
'speak': "<s>Card1</s>"
'weight': 'bolder'
'size': 'large'
},
{
'type': 'Input.Text'
'id': "the-same-id"
'speak': "<s>the same text</s>"
'wrap': true
'style': 'text'
},
{
'type': 'TextBlock'
'text': "This is unique to 1"
'speak': "<s>This is unique to 1</s>"
},
{
"type": "Input.ChoiceSet"
"id": "a-selector-unique-to-card1-id"
"style": "compact"
"choices": [
{
"title": "Card 1 choice"
"value": "Card 1 choice"
},
{
"title": "Another card 1 choice"
"value": "Another card 1 choice"
}
]
"value": "Another card 1 choice"
}
]
}
}
# Test addTextInput
it 'both cards don\'t have bodies, should return card1 unchanged', ->
# Setup
delete card1.content.body
delete card2.content.body
delete expected.content.body
# Test addSelector
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardBody(card1, card2)
).to.not.throw()
# Test createMenuInputCard (*** if use adaptive card for menu, won't need this)
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
# Test getFollowUpButtons
it 'card2 doesn\'t have a body, should return card1 unchanged', ->
# Setup
delete card2.content.body
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardBody(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
it 'card1 doesn\'t have a body, result body should equal card2\'s body', ->
# Setup
delete card1.content.body
expected.content.body = card2.content.body
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardBody(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
it 'both cards have bodies, should combine both bodies into card1 and remove duplicates', ->
# Setup
expected.content.body.push({
'type': 'TextBlock'
'text': "Card2"
'speak': "<s>Card2</s>"
'weight': 'bolder'
'size': 'large'
})
expected.content.body.push({
'type': 'TextBlock'
'text': "This is unique to 2"
'speak': "<s>This is unique to 2</s>"
})
expected.content.body.push({
"type": "Input.ChoiceSet"
"id": "a-selector-unique-to-card2-id"
"style": "compact"
"choices": [
{
"title": "Card 2 choice"
"value": "Card 2 choice"
},
{
"title": "Another card 2 choice"
"value": "Another card 2 choice"
}
]
"value": "Another card 2 choice"
})
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardBody(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
describe 'appendCardActions', ->
card1 = null
card2 = null
expected = null
beforeEach ->
card1 = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"actions": [
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-shared-field": "shared"
}
},
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-field-card1": "a-value-card1"
}
}
]
}
}
card2 = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"actions": [
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-shared-field": "shared"
}
},
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-field-card2": "a-value-card2"
}
},
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-shared-field": "shared"
}
}
]
}
}
expected = {
'contentType': 'application/vnd.microsoft.card.adaptive'
'content': {
"type": "AdaptiveCard"
"version": "1.0"
"actions": [
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-shared-field": "shared"
}
},
{
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-field-card1": "a-value-card1"
}
}
]
}
}
it 'both cards don\'t have actions, should return card1 unchanged', ->
# Setup
delete card1.content.actions
delete card2.content.actions
delete expected.content.actions
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardActions(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
it 'card2 doesn\'t have actions, should return card1 unchanged', ->
# Setup
delete card2.content.actions
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardActions(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
it 'card1 doesn\'t have actions, result actions should equal card2\'s actions', ->
# Setup
delete card1.content.actions
expected.content.actions = card2.content.actions
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardActions(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)
it 'both cards have actions, should combine both actions into card1 and remove duplicates', ->
# Setup
expected.content.actions.push({
'type': 'Action.Submit'
'title': 'Submit'
'speak': '<s>Submit</s>'
'data': {
"a-field-card2": "a-value-card2"
}
})
# Action
result = null
expect(() ->
result = HubotResponseCards.appendCardActions(card1, card2)
).to.not.throw()
# Assert
expect(JSON.stringify(result)).to.eql JSON.stringify(expected)