Rails Test Expects NIL

147 views Asked by At

So I am writing a unit test to test whether a contact is blocked... if it is blocked then I expect the contact to be unable to send a message. Here is backend logic I am trying to test

 if account
            ActsAsTenant.with_tenant(account) do
              contact = create_or_update_smooch_contact external_id, account.account_type, contact_name, nil, app_user[:_id]
              text = message[:text]
              if !contact.blocked 
                @message = Message.new(text: text, message_type: get_message_type(message[:type], message[:mediaType]),
                contact_id: contact.id, external_id: message[:_id], direction: "IN", account_id: account.id)
                
                if message[:quotedMessage]
                  # check if there is a quoted message
                  quoted_content =  message[:quotedMessage][:content]
                  reply_external_id = quoted_content[:_id]

                  quoted_message = Message.find_by(external_id: reply_external_id)
                  @message.reply_id = quoted_message.id if quoted_message
                end
                saved = @message.save!              
  end

Here is the unit test code

    test 'whatsapp blocked contact dont receive message' do
        account = accounts(:smooch_whatsapp)
        
        ActsAsTenant.with_tenant(account) do
            blocked_contact = contacts(:whatsapper)
            blocked_contact.blocked = true
            blocked_contact.save!
            
            before = Conversation.where(account: account).count
            payload = {"trigger": "message:appUser","app": {"_id": "blocked.smooch_app_user_id"},"version": "v1.1","messages": [{"type": "text","text": "Hi","role": "appUser","received": 15592934690.301,"name": "Trey","authorId": "b1f2003cd817cd4yyfaf8285","_id": "5cf0ee1c2c9d37000yy140c4","source": {"type": "whatsapp","integrationId": "smooch_whatsapp_integration_id", "originalMessageId": "ABEGJUcFhmVkAgo6FIL33ADyyeeex_y","originalMessageTimestamp": 1559293468}}],"appUser": {"_id": "b1f2003cd817cd4d6faf8285", "userId": '254712345678',"conversationStarted": true,"givenName": "Trey","signedUpAt": "2019-05-31T09:04:28.266Z","properties": {}},
"conversation": {"_id": "18884d5dbdd138efaa088cc3"},"client": {"integrationId": "5b7810f77c2a2b0022b1f69c","externalId": "blocked.external_id","id": "d776004d-c8d2-4aaa-ba24-96dd65b6ddfcc",
"displayName": "+254 712 345 678","status": "active", "raw": {
"from": '254712345678', "profile": {"name": "Trey"}}}}

            payload[:format] = "json"
            post smooch_messages_path, params: payload
            assert_response :success

            after = Conversation.where(account: account).count

            message = Message.find_by(external_id: "b1f2003cd817cd4d6faf8285")
            assert_nil message
            assert_equal before, after
        end
    end

the test keeps failing because it expects the message to be nil if the contact is blocked

Expected #<Message id: 1030097468, text: "Hi", message_type: "Text", contact_id: 973852195, created_at: "2020-12-03 01:00:13", updated_at: "2020-12-03 01:00:13",image_file_size: nil whatsapp_message_id: nil, direction: "IN", word_processed: false, read_timestamp: nil, external_id: "5cf0ee1c2c9d37000"> to be nil.

how do I fix it??

1

There are 1 answers

0
Adam Smooch On

Adding messages to the conversation record and delivery of those messages to 3rd-party channels are separate operations. Failure of the former will throw a HTTP-error on the request. Failure of the latter will only be communicated via message:delivery:failure webhooks events: https://docs.smooch.io/guide/delivery-events/#delivery-failure