1

My Rails app is using the net sftp gem to connect to a third party server and retrieve files there. The problem is, they will block the connection if we attempt to connect too many times consecutively. Our code retrieves the file once a day at 4:00AM so that's normally not an issue.

But we do have code to test if the retrieved files are parsed correctly, and when we run our tests too many times in a row, the SFTP server will block us out, so another developer on our team cannot work on another feature that needs to connect to the SFTP server anymore. Normally, we stub out the SFTP request, but we recently found out someone forgot to add the stub in one of the tests, so we always get a connection refuse for the past couple days because another one of us is trying to fix some tests and they run the test suites a bunch of times in a row.

Our stub looks like this:

sftp_start = mock
empty_extract = file_fixture("fixture_csv.csv").read
sftp_start.stubs(:download!).returns(empty_extract)
Net::SFTP.stubs(:start).yields(sftp_start)

So, the question is, is there a way to block SFTP connections in the test environment, so if someone did forget to add the stub, we'd get an error instead? I know webmock can block external requests, but it doesn't seem to catch SFTP requests. I did some googling but didn't find anything related to this issue so any suggestions would be nice! We are using minitest to run our tests.

Ccyan
  • 1,057
  • 12
  • 32
  • you can use WebMock gem, and in your `spec_helper.rb` file disable any external request with `WebMock.disable_net_connect!(allow_localhost: true)`. – fanta Jan 04 '19 at 22:27
  • We are using that in our test, but it doesn't seem to block SFTP connections, only net connections. – Ccyan Jan 04 '19 at 22:51
  • 1
    you could try monkey patching the SFTP method that actually creates the connection method to raise an exception instead in your spec_helper.rb. something like `class SFTP def connect raise "don't do this in the test env" end end` This would break the SFTP gem in your test environment, so it shouldn't be able to create a connection – baseballlover723 Jan 04 '19 at 23:09
  • The address of the STFP server should be defined in one location only, and there should be only one method to retrieve it. Make that method returns the address of a local STFP server when not in production. – philant Jan 06 '19 at 18:15

0 Answers0