From 3da9968a7226cf0ca973038dd1cf4d1813bc405e Mon Sep 17 00:00:00 2001 From: Melanie Mendoza Date: Tue, 14 Aug 2018 17:27:29 -0700 Subject: [PATCH] Added tests for adapter.coffee, adapter-middleware, msteams-middleware, and hubot-response-cards --- src/adapter.coffee | 4 +- src/hubot-response-cards.coffee | 12 +- src/msteams-middleware.coffee | 3 - test/adapter-middleware.test.coffee | 42 ++ test/adapter.test.coffee | 37 +- test/hubot-response-cards.test.coffee | 943 +++++++++++++++++++------- 6 files changed, 740 insertions(+), 301 deletions(-) diff --git a/src/adapter.coffee b/src/adapter.coffee index cc8a71f..0f24243 100644 --- a/src/adapter.coffee +++ b/src/adapter.coffee @@ -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 diff --git a/src/hubot-response-cards.coffee b/src/hubot-response-cards.coffee index ac044e5..bcf619a 100644 --- a/src/hubot-response-cards.coffee +++ b/src/hubot-response-cards.coffee @@ -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 diff --git a/src/msteams-middleware.coffee b/src/msteams-middleware.coffee index 1db9c73..9c6e16e 100644 --- a/src/msteams-middleware.coffee +++ b/src/msteams-middleware.coffee @@ -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 = diff --git a/test/adapter-middleware.test.coffee b/test/adapter-middleware.test.coffee index 9fbecef..3311602 100644 --- a/test/adapter-middleware.test.coffee +++ b/test/adapter-middleware.test.coffee @@ -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" + } + + + diff --git a/test/adapter.test.coffee b/test/adapter.test.coffee index 72a01c1..20eeaf8 100644 --- a/test/adapter.test.coffee +++ b/test/adapter.test.coffee @@ -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: 'Bot do something Bot and tell User 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" \ No newline at end of file diff --git a/test/hubot-response-cards.test.coffee b/test/hubot-response-cards.test.coffee index 4ac13c7..370774a 100644 --- a/test/hubot-response-cards.test.coffee +++ b/test/hubot-response-cards.test.coffee @@ -1,293 +1,708 @@ +# 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 - query = null - response = { - type: 'message', - text: 'The team: `team-name` was successfully created', - address: { - id: 'id', - channelId: 'msteams', - user: { - id: 'user-id', - name: 'user-name', - aadObjectId: 'user-aad-id' - }, - conversation: { - conversationType: 'conversation-type', - id: 'conversation-id' - }, - bot: { - id: 'botframework-bot-id', - name: 'botframework-bot-name' - }, - serviceUrl: 'a-service-url' - } - } - - it 'should not construct card', -> - # Setup - query = 'hubot ping' - - # Action and Assert - expect(() -> - card = HubotResponseCards.maybeConstructCard(response, query) - expect(card).to.be.null - ).to.not.throw() - - it 'should construct card', -> - # Setup - query = 'hubot gho create team team-name' - followUp1 = 'gho add (members|repos) to team ' - followUp2 = 'gho list (teams|repos|members)' - followUp3 = 'gho delete team ' - - - # 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': { - "type": "AdaptiveCard" - "version": "1.0" - "body": [ - { - 'type': 'TextBlock' - 'text': "#{query}" - 'speak': "#{query}" - 'weight': 'bolder' - 'size': 'medium' +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', + address: { + id: 'id', + channelId: 'msteams', + user: { + id: 'user-id', + name: 'user-name', + aadObjectId: 'user-aad-id' + userPrincipalName: 'user-UPN' }, - { - 'type': 'TextBlock' - 'text': "#{response.text}" - 'speak': "#{response.text}" - } - ], - 'actions': [ - { - "title": "gho add " - "type": "Action.ShowCard" - "card": { - "type": "AdaptiveCard" - "body": [ - { - 'type': 'TextBlock' - 'text': "gho add " - 'speak': "gho add " - 'weight': 'bolder' - 'size': 'large' - }, - { - 'type': 'TextBlock' - 'text': 'Add members or repos?' - 'speak': "Add members or repos?" - }, - { - "type": "Input.ChoiceSet" - "id": "#{followUp1} - input0" - "style": "compact" - "value": "members" - "choices": [ - { - "title": "members" - "value": "members" - }, - { - "title": "repos" - "value": "repos" - } - ] - }, - { - 'type': 'TextBlock' - 'text': 'Input a comma separated list to add' - 'speak': "Input a comma separated list to add" - }, - { - 'type': 'Input.Text' - 'id': "#{followUp1} - input1" - 'speak': 'Input a comma separated list to add' - 'wrap': true - 'style': 'text' - 'maxLength': 1024 - }, - { - 'type': 'TextBlock' - 'text': 'What is the name of the team to add to?' - 'speak': "What is the name of the team to add to?" - }, - { - 'type': 'Input.Text' - 'id': "#{followUp1} - input2" - 'speak': 'What is the name of the team to add to?' - 'wrap': true - 'style': 'text' - 'maxLength': 1024 - } - ], - 'actions': [ - { - 'type': 'Action.Submit' - 'title': 'Submit' - 'speak': 'Submit' - 'data': { - 'queryPrefix': "#{followUp1}" - "#{followUp1} - query0": 'hubot gho add ' - "#{followUp1} - query1": ' ' - "#{followUp1} - query2": ' to team ' - } - } - ] + conversation: { + conversationType: 'conversation-type', + id: 'conversation-id' + }, + bot: { + id: 'botframework-bot-id', + name: 'botframework-bot-name' + }, + serviceUrl: 'a-service-url' + } + } + + it 'should not construct response card for the query', -> + # Setup + query = 'hubot ping' + + # Action + card = null + expect(() -> + card = HubotResponseCards.maybeConstructResponseCard(response, query) + ).to.not.throw() + + # 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) to team ' + followUp2 = 'gho list (teams|repos|members)' + followUp3 = 'gho delete team ' + expected = { + 'contentType': 'application/vnd.microsoft.card.adaptive' + 'content': { + "type": "AdaptiveCard" + "version": "1.0" + "body": [ + { + 'type': 'TextBlock' + 'text': "#{query}" + 'speak': "#{query}" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'TextBlock' + 'text': "#{response.text}" + 'speak': "#{response.text}" } - }, - { - "title": "gho list " - "type": "Action.ShowCard" - "card": { - "type": "AdaptiveCard" - "body": [ + ], + 'actions': [ + { + "title": "gho add" + "type": "Action.ShowCard" + "card": { + "type": "AdaptiveCard" + "body": [ + { + 'type': 'TextBlock' + 'text': "gho add" + 'speak': "gho add" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'TextBlock' + 'text': 'Add members or repos?' + 'speak': "Add members or repos?" + }, + { + "type": "Input.ChoiceSet" + "id": "#{followUp1} - input0" + "style": "compact" + "value": "members" + "choices": [ + { + "title": "members" + "value": "members" + }, + { + "title": "repos" + "value": "repos" + } + ] + }, + { + 'type': 'TextBlock' + 'text': 'Input a comma separated list to add' + 'speak': "Input a comma separated list to add" + }, + { + 'type': 'Input.Text' + 'id': "#{followUp1} - input1" + 'speak': 'Input a comma separated list to add' + 'wrap': true + 'style': 'text' + 'maxLength': 1024 + }, + { + 'type': 'TextBlock' + 'text': 'What is the name of the team to add to?' + 'speak': "What is the name of the team to add to?" + }, + { + 'type': 'Input.Text' + 'id': "#{followUp1} - input2" + 'speak': 'What is the name of the team to add to?' + 'wrap': true + 'style': 'text' + 'maxLength': 1024 + } + ], + 'actions': [ + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + 'data': { + 'queryPrefix': "#{followUp1}" + "#{followUp1} - query0": 'hubot gho add ' + "#{followUp1} - query1": ' ' + "#{followUp1} - query2": ' to team ' + } + } + ] + } + }, + { + "title": "gho list" + "type": "Action.ShowCard" + "card": { + "type": "AdaptiveCard" + "body": [ + { + 'type': 'TextBlock' + 'text': "gho list" + 'speak': "gho list" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'TextBlock' + 'text': 'List what?' + 'speak': "List what?" + }, + { + "type": "Input.ChoiceSet" + "id": "#{followUp2} - 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': 'Submit' + 'data': { + 'queryPrefix': "#{followUp2}" + "#{followUp2} - query0": 'hubot gho list ' + } + } + ] + } + }, + { + "title": "gho delete team" + "type": "Action.ShowCard" + "card": { + "type": "AdaptiveCard" + "body": [ + { + 'type': 'TextBlock' + 'text': "gho delete team" + 'speak': "gho delete team" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'TextBlock' + 'text': 'What is the name of the team to delete? (Max 1024 characters)' + 'speak': "What is the name of the team to delete? (Max 1024 characters)" + }, + { + 'type': 'Input.Text' + 'id': "#{followUp3} - input0" + 'speak': "What is the name of the team to delete? (Max 1024 characters)" + 'wrap': true + 'style': 'text' + 'maxLength': 1024 + } + ], + 'actions': [ + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + 'data': { + 'queryPrefix': "#{followUp3}" + "#{followUp3} - query0": 'hubot gho delete team ' + } + } + ] + } + } + ] + } + } + + # Action + card = null + expect(() -> + card = HubotResponseCards.maybeConstructResponseCard(response, query) + ).to.not.throw() + + # Assert + expect(card).to.eql(expected) + + describe 'maybeConstructMenuInputCard', -> + it 'should not construct menu input card for the query', -> + # Setup + query = 'ping' + + # Action + result = null + expect(() -> + result = HubotResponseCards.maybeConstructMenuInputCard(query) + ) + + # 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': "gho list" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'TextBlock' + 'text': 'List what?' + 'speak': "List what?" + }, + { + "type": "Input.ChoiceSet" + "id": "gho list (teams|repos|members) - input0" + "style": "compact" + "value": "teams" + "choices": [ { - 'type': 'TextBlock' - 'text': "gho list " - 'speak': "gho list " - 'weight': 'bolder' - 'size': 'large' - }, - { - 'type': 'TextBlock' - 'text': 'List what?' - 'speak': "List what?" - }, - { - "type": "Input.ChoiceSet" - "id": "#{followUp2} - input0" - "style": "compact" + "title": "teams" "value": "teams" - "choices": [ - { - "title": "teams" - "value": "teams" - }, - { - "title": "repos" - "value": "repos" - }, - { - "title": "members" - "value": "members" - } - ] - } - ], - 'actions': [ + }, { - 'type': 'Action.Submit' - 'title': 'Submit' - 'speak': 'Submit' - 'data': { - 'queryPrefix': "#{followUp2}" - "#{followUp2} - query0": 'hubot gho list ' - } + "title": "repos" + "value": "repos" + }, + { + "title": "members" + "value": "members" } ] } + ], + 'actions': [ + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + 'data': { + 'queryPrefix': "gho list (teams|repos|members)" + "gho list (teams|repos|members) - query0": 'hubot gho list ' + } + } + ] + } + } + + # Action + result = null + expect(() -> + result = HubotResponseCards.maybeConstructMenuInputCard(query) + ).to.not.throw() + + # Assert + expect(result).to.eql(expected) + + 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': "Card1" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'Input.Text' + 'id': "the-same-id" + 'speak': "the same text" + 'wrap': true + 'style': 'text' + }, + { + 'type': 'TextBlock' + 'text': "This is unique to 1" + 'speak': "This is unique to 1" + }, + { + "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': "Card2" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'TextBlock' + 'text': "This is unique to 2" + 'speak': "This is unique to 2" + }, + { + 'type': 'Input.Text' + 'id': "the-same-id" + 'speak': "the same text" + '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': "Card1" + 'weight': 'bolder' + 'size': 'large' + }, + { + 'type': 'Input.Text' + 'id': "the-same-id" + 'speak': "the same text" + 'wrap': true + 'style': 'text' + }, + { + 'type': 'TextBlock' + 'text': "This is unique to 1" + 'speak': "This is unique to 1" + }, + { + "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" + } + ] + } + } + + it 'both cards don\'t have bodies, should return card1 unchanged', -> + # Setup + delete card1.content.body + delete card2.content.body + delete expected.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 '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': "Card2" + 'weight': 'bolder' + 'size': 'large' + }) + expected.content.body.push({ + 'type': 'TextBlock' + 'text': "This is unique to 2" + 'speak': "This is unique to 2" + }) + 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": "gho delete team " - "type": "Action.ShowCard" - "card": { - "type": "AdaptiveCard" - "body": [ - { - 'type': 'TextBlock' - 'text': "gho delete team " - 'speak': "gho delete team " - 'weight': 'bolder' - 'size': 'large' - }, - { - 'type': 'TextBlock' - 'text': 'What is the name of the team to delete? (Max 1024 characters)' - 'speak': "What is the name of the team to delete? (Max 1024 characters)" - }, - { - 'type': 'Input.Text' - 'id': "#{followUp3} - input0" - 'speak': "What is the name of the team to delete? (Max 1024 characters)" - 'wrap': true - 'style': 'text' - 'maxLength': 1024 - } - ], - 'actions': [ - { - 'type': 'Action.Submit' - 'title': 'Submit' - 'speak': 'Submit' - 'data': { - 'queryPrefix': "#{followUp3}" - "#{followUp3} - query0": 'hubot gho delete team ' - } - } - ] - } + "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': 'Submit' + 'data': { + "a-shared-field": "shared" + } + }, + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + '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': 'Submit' + 'data': { + "a-shared-field": "shared" + } + }, + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + 'data': { + "a-field-card2": "a-value-card2" + } + }, + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + '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': 'Submit' + 'data': { + "a-shared-field": "shared" + } + }, + { + 'type': 'Action.Submit' + 'title': 'Submit' + 'speak': 'Submit' + 'data': { + "a-field-card1": "a-value-card1" + } + } + ] + } } - } - 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" + 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 - # card = initializeAdaptiveCard(text) + # Action + result = null + expect(() -> + result = HubotResponseCards.appendCardActions(card1, card2) + ).to.not.throw() - # # Assert - # expected = { - # 'contentType': 'application/vnd.microsoft.card.adaptive' - # 'content': { - # "type": "AdaptiveCard" - # "version": "1.0" - # } - # 'body': [ - # { - # 'type': 'TextBlock' - # 'text': "#{text}" - # 'speak': "#{text}" - # 'weight': 'bolder' - # 'size': 'medium' - # } - # ] - # } - # expect(card).to.equal(expected) + # 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 - # # Test addTextBlock - # it 'should add a TextBlock', -> - # # Setup + # Action + result = null + expect(() -> + result = HubotResponseCards.appendCardActions(card1, card2) + ).to.not.throw() - # # Action + # Assert + expect(JSON.stringify(result)).to.eql JSON.stringify(expected) - # # Assert + it 'card1 doesn\'t have actions, result actions should equal card2\'s actions', -> + # Setup + delete card1.content.actions + expected.content.actions = card2.content.actions - # Test addTextInput + # Action + result = null + expect(() -> + result = HubotResponseCards.appendCardActions(card1, card2) + ).to.not.throw() - # Test addSelector + # Assert + expect(JSON.stringify(result)).to.eql JSON.stringify(expected) - # Test createMenuInputCard (*** if use adaptive card for menu, won't need this) + 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': 'Submit' + 'data': { + "a-field-card2": "a-value-card2" + } + }) - # Test getFollowUpButtons \ No newline at end of file + # Action + result = null + expect(() -> + result = HubotResponseCards.appendCardActions(card1, card2) + ).to.not.throw() + + # Assert + expect(JSON.stringify(result)).to.eql JSON.stringify(expected) \ No newline at end of file