Freecodecamp: Add tests for backend projects

Created on 10 Oct 2019  路  21Comments  路  Source: freeCodeCamp/freeCodeCamp

I am making a new issue to track the progress of this. Most of the backend projects have no tests; allowing any valid URL to be submitted to pass - more discussion here https://github.com/freeCodeCamp/freeCodeCamp/issues/36467

The purpose of creating tests here is simply to have something there, even if it's just one test, to make sure you can't just submit any random URL. Ideally, the tests would be thorough; but just one would do good enough for now, I think.

APIs and Microservices

  • [x] [Timestamp](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/timestamp-microservice.md) - tests already exist
  • [x] [Header Parser](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/request-header-parser-microservice.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/37187
  • [x] [URL Shortener](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/39311
  • [x] [Exercise Tracker](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/37696
  • [x] [File Metadata](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/file-metadata-microservice.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/39625

Quality Assurance

  • [x] [Metric Imperial Converter](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/06-quality-assurance/quality-assurance-projects/metric-imperial-converter.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/37891
  • [ ] [Issue Tracker](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/06-quality-assurance/quality-assurance-projects/issue-tracker.md) - PR created - https://github.com/freeCodeCamp/freeCodeCamp/pull/39627
  • [ ] [Personal Library](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/06-quality-assurance/quality-assurance-projects/personal-library.md) - PR created - https://github.com/freeCodeCamp/freeCodeCamp/pull/39642
  • [x] [Sudoku Solver](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/06-quality-assurance/quality-assurance-projects/sudoku-solver.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/39816
  • [x] [American British Translator](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/06-quality-assurance/quality-assurance-projects/american-british-translator.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/39822

Scientific Computing with Python

  • [ ] [Arithmetic Formatter](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/07-scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter.md)
  • [ ] [Time Calculator](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/07-scientific-computing-with-python/scientific-computing-with-python-projects/time-calculator.md)
  • [ ] [Budget App](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/07-scientific-computing-with-python/scientific-computing-with-python-projects/budget-app.md)
  • [ ] [Polygon Area Calculator](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/07-scientific-computing-with-python/scientific-computing-with-python-projects/polygon-area-calculator.md)
  • [ ] [Probability Calculator](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/07-scientific-computing-with-python/scientific-computing-with-python-projects/probability-calculator.md)

Data Analysis with Python

  • [ ] [Mean-Variance-Standard Deviation Calculator](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/08-data-analysis-with-python/data-analysis-with-python-projects/mean-variance-standard-deviation-calculator.md)
  • [ ] [Demographic Data Analyzer](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/08-data-analysis-with-python/data-analysis-with-python-projects/demographic-data-analyzer.md)
  • [ ] [Medical Data Visualizer](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/08-data-analysis-with-python/data-analysis-with-python-projects/medical-data-visualizer.md)
  • [ ] [Page View Time Series Visualizer](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/08-data-analysis-with-python/data-analysis-with-python-projects/page-view-time-series-visualizer.md)
  • [ ] [Sea Level Predictor](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/08-data-analysis-with-python/data-analysis-with-python-projects/sea-level-predictor.md)

Infosec

  • [ ] [Stock Price Checker](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/09-information-security/information-security-projects/stock-price-checker.md) - PR created - https://github.com/freeCodeCamp/freeCodeCamp/pull/39640
  • [x] [Anonymous Message Board](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/09-information-security/information-security-projects/anonymous-message-board.md) - PR merged - https://github.com/freeCodeCamp/freeCodeCamp/pull/39652
  • [ ] [Port Scanner](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/09-information-security/information-security-projects/port-scanner.md)
  • [ ] [SHA-1 Password Cracker](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/09-information-security/information-security-projects/sha-1-password-cracker.md)
  • [ ] [Secure Real Time Multiplayer Game](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/09-information-security/information-security-projects/secure-real-time-multiplayer-game.md)

Machine Leaning with Python

  • [ ] [Rock Paper Scissors](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/11-machine-learning-with-python/machine-learning-with-python-projects/rock-paper-scissors.md)
  • [ ] [Cat and Dog Image Classifier](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/11-machine-learning-with-python/machine-learning-with-python-projects/cat-and-dog-image-classifier.md)
  • [ ] [Book Recommendation Engine using KNN](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/11-machine-learning-with-python/machine-learning-with-python-projects/book-recommendation-engine-using-knn.md)
  • [ ] [Linear Regression Health Costs Calculator](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/11-machine-learning-with-python/machine-learning-with-python-projects/linear-regression-health-costs-calculator.md)
  • [ ] [Neural Network SMS Text Classifier](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/11-machine-learning-with-python/machine-learning-with-python-projects/neural-network-sms-text-classifier.md)

Edit: I have updated this to include all the new projects - many of them may not be testable on our end - perhaps just one to make sure they don't submit the example project

help wanted learn bug

All 21 comments

Any suggestion on how to write a test here? I assume there aren't any examples I could look at.

There are some examples @lkreimann - I made a PR for the header parser project, linked above - the timestamp project has tests written, as well. And if you read through the other issue I linked, there's some more info to find.

These projects could use a little TLC in my opinion - it may be difficult to write tests for some of them.

I would like to do this. Could you point me in the right direction? And do you think it should be split into two PRs? One for Microservices and one for infosec?

@jenlky it would be ideal if you could create a PR for each challenge you add tests for. Doing so makes the review process easier. Just reference this issue, so we know which PRs are still needed.

As for how to go about it, take a look at @moT01's PR. He wrote tests that verify aspects of the project's api, when given a project url. So, the project is supposed to return a JSON object with the keys ipaddress, language and software and the three tests make sure each key is present.

Just ask if you need any clarification.

The problem here is that many, or at least some, of the projects don't specify route names; so, in some cases, campers can use any routes they choose - making it impossible to hit them predictably. I mentioned this here. So we may need to go into each project and clarify instructions so that the routes and parameters are predictable.

Yep. I don't see any other way of making them testable, so I think they need a concrete API as part of the project. If it's not obvious what the API for a project should be, maybe we could discuss that here?

I know that @cmccormack and maybe others were working on unifying a repo for the backend projects for a little while. I'm wondering if we should add that to fCC and use it to make changes to the projects. Then we can just import all the code from there to the current glitch projects - I think that was the plan when work was being done there. I'm not sure how much deviation there's been on that repo and what we now have.

@moT01 I want to check if the user is actually redirected to a certain URL. To do that in Nodejs, I will need to require('url'). I can chain the code in the test case but it would be very messy and I prefer not to. Since the file is markdown, where should I require then?

require is out, I'm afraid. The tests run in the browser's JavaScript engine, not node.

What you can do is use fetch. So, something like

    testString: 'getUserInput => fetch(getUserInput(''url'') + ''/redirect'').then(res => {
      if(!res.redirected) { throw "should be redirected"} }, error => { throw error })'

is how I'd check for redirects. You can also get the url with res.url to verify it's redirecting to the right place.

@ojeytonwilliams For URL Shortener, I need to POST URL (google for example) to shorten it and
it should return { "original_url": "www.google.com", "short_url": 1 }. When I access $.get(getUserInput(''url'') + ''/api/shorturl/1''), I should be redirected to google.com.

I will need to either 1) store the original_url as a variable OR 2) POST and then GET in one function. Is there a way to do the first one because that would be cleaner and more readable? And where does the first getUserInput get called from actually?

Here's an excerpt of my test, second test incomplete:

tests:
  - text: 'It should handle a URL as parameter and return shortened URL'
    testString: 'getUserInput => $.post(getUserInput('url') + '/api/shorturl/new').then(data => assert.exists(data.short_url), xhr => { throw new Error(xhr.responseText)})'
  - text: 'It should handle a shortened URL and redirect to original link'
    testString: 'getUserInput => $.get(getUserInput(''url'') + ''/api/shorturl/3'').then()

It occurs to me that we should probably add tests that reject the Glitch project we provide for each project.

tests:
  - text: 'You should supply your own project, not the example url'
    testString: 'getUserInput => assert(!(new RegExp( ".*/thread-paper\.glitch\.me\.*")).test(getUserInput("url")))'

Something like that.

FYI - there are some tests written here - There's no confirmation that they work or are complete, but it's a good place for examples.

Hey, I would be interested in working on this issue by adding tests for URL shortener.

Thanks @Aadhar3, PRs are definitely welcome.

it isn't tracked here (in the initial comment) for some reason, but there appears to already be a #37588 for the url shortener tests.

The two existing pull requests (header parser and exercise tracker) have reviews and approvals, and are awaiting merge afaik.

Oh alright, i do not think there are tests for the File Metadata microservice.. I would like to contribute to that

@Aadhar3 There are no other open PRs for the File Metadata Microservice, so feel free to submit a PR.

I had a general question, on https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/file-metadata-microservice, why does a user have to submit both a glitch link and a github link?

That's just how it is at the moment @Aadhar3 - There's an issue that plans to make the github link optional.

FYI - there are some tests written here - There's no confirmation that they work or are complete, but it's a good place for examples.

Just FYI...File metadata test is working but it doesn't check if the user submitted the sample glitch.

Just to make sure this does not get overlooked - it looks like the infosec projects are now the quality assurance projects, and the "Stock Price Checker" and "Anonymous Message Board" were replaced by the "Sudoku Solver" and "American British Translator", respectively, and moved down to the infosec section at the end of the curriculum. 馃檪

Was this page helpful?
0 / 5 - 0 ratings