<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mateusz Niezgoda on Medium]]></title>
        <description><![CDATA[Stories by Mateusz Niezgoda on Medium]]></description>
        <link>https://medium.com/@mateuszniezgoda?source=rss-70f9e5cc5792------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*iKniuK4dBdqZq7ZkBfNMjQ.jpeg</url>
            <title>Stories by Mateusz Niezgoda on Medium</title>
            <link>https://medium.com/@mateuszniezgoda?source=rss-70f9e5cc5792------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:44:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mateuszniezgoda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[TEST DRIVEN DEVELOPMENT]]></title>
            <link>https://medium.com/@mateuszniezgoda/test-driven-development-947e4abe2bd4?source=rss-70f9e5cc5792------2</link>
            <guid isPermaLink="false">https://medium.com/p/947e4abe2bd4</guid>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Mateusz Niezgoda]]></dc:creator>
            <pubDate>Sat, 25 Jan 2020 14:36:08 GMT</pubDate>
            <atom:updated>2020-01-27T12:49:55.449Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to engineer reliable software</h3><h3>Yet another article about TDD.</h3><p>Ten artykuł jest również dostępny w języku polskim <a href="https://www.linkedin.com/pulse/test-driven-development-mateusz-niezgoda/">na LinkedIn</a>.</p><p>This article is made from the lightning talk that I have presented to my work colleagues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZRRwwNe0f21mfDzO.jpeg" /><figcaption>Test Driven Development<br>How to engineer reliable software.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qg807O7Wu5m-4e5_.jpeg" /><figcaption>Why should I write tests?</figcaption></figure><h3>Why should you write tests?</h3><p>Firstly: tests allow us to write code that is cleaner because the engineer is focused on goals that were presented by “business” (product owner/client).</p><p>When your task is to find the word “klapaucius” in the text you are just going to use String.includes in JavaScript rather than analyzing text using various machine learning models.</p><p>Another advantage is the higher quality of the code. The code is tested so we can refactor it, introducing improved architecture — without worrying that other important parts of the system will be broken by our changes. We are going to see errors before deploying an application on production and being woke up by calls from users.</p><p>If we have the tests — we do not need to test our work manually. Most of the problems will occur while running test commands. Of course, we should “point and click” in our software — just in case — on staging or pre-prod. However, we do not need to use tools like Postman as often. When we spent less time on manual testing we have more time to write “The Meat” — software that actually is doing something valuable to the business and therefore we are going to deliver more at the same time.</p><p>When some tests are written and we do not need to manually check everything every time — with high probability we will try to have as high code coverage as possible — because we (as humans) are lazy and we love to make things simpler and faster. With higher coverage, we also have better documentation. Yes — tests can act as a book for new developers as well as for us after some time when we don’t remember what that particular part of code was doing.</p><p>Automated tests will outcome in less fails on production because most of “bad code” is going to show up before deployment as the red screen will appear saying “test failed”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6vql_g1XEbU8UJHz.jpeg" /><figcaption>It’s also about the velocity — How fast am I going to deliver software?</figcaption></figure><p>Writing tests in the long-term is going to allow write more code than in the non-TDD approach. We need more time to set-up tests and build configurations; however, later, we do not spend so much time on regression tests — testing again-and-again the same code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mQajrXskDlvzkQUx.jpeg" /><figcaption>How it helps me to engineer better software?</figcaption></figure><h3>How exactly writing tests is going to help me engineer better software?</h3><p>You do not need to spend as much time on debugging and testing as the tests are going to do it for you due to automation and simplicity of running them.</p><p>Your focus is on the goal rather than thinking about architecture. Not that architecture is not important. It is! Just use patterns and “big architecture” when it’s needed; not on simple, less important methods.</p><p>With tests, the specification is created. The tests show what business aspects your application is capable to handle and what is not.</p><p>When you are writing unit tests you are going to see that it’s better to inject objects rather than creating them inside classes. And that not coupling code so tightly is better because you can test more cases and do it simpler.</p><p>Refactoring is no longer going to scare your team. You have tests that cover multiple cases that would never be tested otherwise. You can introduce better architecture, change how things work without worrying that other part of the system is going to fail for your clients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CsSP58SZLjRbW-SV.jpeg" /><figcaption>An old — traditional approach to software engineering.</figcaption></figure><h3>Traditional, non-TDD approach.</h3><p>In non-TDD approach typically our workflow would look like this:</p><p>We are given business requirements. With the team, we are trying to sketch how the solution will look like and then we are dividing the problem into subtasks.</p><p>Engineers are taking tasks, start to code “the meat” and in the meantime or after some tests are written. However, in most cases, the number of tests approaches zero in this “model”. All of the testing is done by hands rather than automatically.</p><p>When somebody — or we — want to make small changes in the code — we need to repeat the test process manually again.</p><p>There is a better way!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L5iL8zEllitReUVR.jpeg" /><figcaption>How to engineer software in TDD?</figcaption></figure><h3>Test-driven approach to engineering software.</h3><p>In the TDD you still have the design phase where you are going to discuss the problem and design the architecture. But now there is a new aspect: you should also take on the board test scenarios that are important to your business.</p><p>When you have a task you should start not from coding business logic but rather start with writing code that will test your future work. And yes — when you run new tests for the first time all of them should fail and be red.</p><p>This way of working gives you another advantage: you do not need an internet connection(very often) nor the container on the cloud. Just your device with simple Node.JS and packages installed or other that you are using on your environment. No database, cache nor third party services. One command and most times you know whenever what you have written will work on production.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*y76VzYSjasp_GtOI.jpeg" /><figcaption>Development in TDD in practice.</figcaption></figure><h3>Red » Green » Refactor</h3><p>Start with the test — it should fail at the beginning. Then write just enough code to pass the test. When the message is “test passed” with a green background start refactoring code and play with architecture design.</p><p>Of course in real life, you will rather write a few tests or more code than required by test — it’s all about the balance — the outcome should be that you do not need to do manual verification whenever everything is working properly 🙂</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ka-k_cxvr-SKYIvv.jpeg" /><figcaption>How you should not do it.</figcaption></figure><h3>Things to avoid while writing unit tests.</h3><p>Unit tests must be fast — you cannot use anything slow. Therefore you shouldn’t use a database, call 3-rd party service neither make requests to API that is owned by you. If you need to — mock such service.</p><p>You shouldn’t use filesystem nor bash nor Bluetooth Xbox controller via /dev/btjoy.</p><p>Your tests must be independent of the platform that they are running on. You shouldn’t use parallel parts, that could affect the way other test works.</p><p>Treat tests as independent entities. As tests must be fast — you cannot purposefully slow them down using setTimeouts or sleeps — in <a href="https://jestjs.io/docs/en/timer-mocks">Jest you can fake them</a> so they are instant.</p><p>And if you shouldn’t touch files you also shouldn’t mess with environmental variables nor configurations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8Hl0R6vcbItYsbDm.jpeg" /><figcaption>Tests F.I.R.S.T.</figcaption></figure><h3>Tests should be F.I.R.S.T</h3><p>That famous acronym means that the tests must be as fast as possible — to not discourage developers from running them. This must take a few seconds and not minutes nor hours 🙂</p><p>To make tests fast they must run at the same time(parallel) so one test must be independent of another. Every test should have own dataset. Tests should be written in such a way that they can be randomly run with no order — therefore for example date set by one test cannot be used by the next test.</p><p>Tests should be self-validating so we do not need to manually check results. We should get just a red(fail) or green(pass) outcome.</p><p>And last but not least — we are not writing tests just to cover 100% of the code. The tests should cover important business scenarios and edge-cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v3JynlvJgQBAMWg_.jpeg" /><figcaption>The tools: TypeScript, Jest, SonarQube, and Bamboo.</figcaption></figure><p>The tools are used in projects that I am working on. Below you have them briefly described.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lSGO1IX7TQQylMWF.jpeg" /><figcaption>Tools: TypeScript.</figcaption></figure><p>TypeScript is a superset on top of JavaScript. It supports typed variables that are checked before transpilaion to JavaScript and allows you to decrease the number of errors due to typo or wrong parameter order. It has a great community, support of future JavaScript features(aka ES-Next). It helps to create a cleaner, higher-quality code. Due to typed variables refactoring is easier. TypeScript transpiles to the version of JS that you will choose whenever you want to support for example older browsers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XNGRgaAfD2q7jFfP.jpeg" /><figcaption>Tools: Jest — example test scenario.</figcaption></figure><p>Jest is a testing framework that is fast, simple to use and has a ton of options to check whenever the data is correctly processed. It also has great documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PK7bKGzaeSxvAQjw.jpeg" /><figcaption>Tools: Jest — available asserts.</figcaption></figure><p>Jest has tons of assertions that you can use. You can assert value, search in strings, arrays, objects or go deeper and spy on calls to methods. Just to name a few.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1zYegKUq2lTSwRwE.jpeg" /><figcaption>Tools: Jest — Code Coverage.</figcaption></figure><p>Another advantage of Jest is a code coverage report. You can check whenever you have tested a particular file, branch(if/else statement) or line. Remember that you shouldn’t just try to achieve as high coverage as possible — most important part is to cover crucial business scenarios 🙂</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_5QOCduKYZnf907f.jpeg" /><figcaption>Tools: SonarQube</figcaption></figure><p>SonarQube is the tool that is doing static code analysis. It checks whenever your code has bugs, code smells or security vulnerabilities. It supports multiple languages including JavaScript and TypeScript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tzZSw1zpdfQwMOXS.jpeg" /><figcaption>Tools: Bamboo</figcaption></figure><p>Bamboo is a tool that allows us to connect each part of the deployment process. You can configure it in such a way that on every commit Bamboo will run unit tests, code linters and run the SonarQube analysis. We are using a connection between Bamboo and BitBucket (GIT repository). We are not allowing deploy nor merge to master when tests are not passing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FtK7uIdzRYBp8z0y.jpeg" /><figcaption>TDD by example — live coding session.</figcaption></figure><p>There was TDD Live Coding Session with examples that are available on <a href="https://github.com/niezgoda/LT">https://github.com/niezgoda/LT</a></p><h3>Live Coding Video</h3><p>I have created a video from coding simple Fizz Buzz in TDD-style.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FOauAekOk0W4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOauAekOk0W4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FOauAekOk0W4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4799d34300ed49fcafea657bbe62afe/href">https://medium.com/media/f4799d34300ed49fcafea657bbe62afe/href</a></iframe><p>You have questions or would like to give me feedback?<br>You are welcome! Just write to <a href="/service/mailto:mateusz@niezgoda.io">mateusz@niezgoda.io</a> or find me on <a href="https://www.facebook.com/mateusz.niezgoda">Facebook</a> / <a href="https://www.instagram.com/niezgoda.io/">Instagram</a>.</p><h3>Sources &amp; more</h3><ul><li><a href="https://github.com/niezgoda/LT">https://github.com/niezgoda/LT</a></li><li><a href="https://www.typescriptlang.org/docs/home.html">https://www.typescriptlang.org/docs/home.html</a></li><li><a href="https://jestjs.io/docs/en/getting-started">https://jestjs.io/docs/en/getting-started</a></li><li><a href="https://docs.sonarqube.org/latest/">https://docs.sonarqube.org/latest/</a></li><li><a href="https://www.atlassian.com/software/bamboo">https://www.atlassian.com/software/bamboo</a></li><li><a href="https://www.atlassian.com/software/bitbucket">https://www.atlassian.com/software/bitbucket</a></li><li><a href="https://www.jamesshore.com/Agile-Book/test_driven_development.html">https://www.jamesshore.com/Agile-Book/test_driven_development.html</a></li><li><a href="https://www.slideshare.net/dehringer/test-driven-development-5785229">https://www.slideshare.net/dehringer/test-driven-development-5785229</a></li><li><a href="https://www.slideshare.net/CodeOps/an-introduction-to-test-driven-development">https://www.slideshare.net/CodeOps/an-introduction-to-test-driven-development</a></li><li><a href="https://docs.pact.io/best_practices/consumer/contract_tests_not_functional_tests">https://docs.pact.io/best_practices/consumer/contract_tests_not_functional_tests</a></li><li><a href="https://martinfowler.com/bliki/ContractTest.html">https://martinfowler.com/bliki/ContractTest.html</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=947e4abe2bd4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Every line (of the code) matters]]></title>
            <link>https://medium.com/@mateuszniezgoda/every-line-of-the-code-matters-1836648eb2af?source=rss-70f9e5cc5792------2</link>
            <guid isPermaLink="false">https://medium.com/p/1836648eb2af</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[vulnerability]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Mateusz Niezgoda]]></dc:creator>
            <pubDate>Sun, 12 Jan 2020 18:49:26 GMT</pubDate>
            <atom:updated>2020-01-12T18:49:26.988Z</atom:updated>
            <content:encoded><![CDATA[<h3>How many vulnerabilities can three lines of code introduce</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ckskQZXirnWpd-_l4i1fnA.jpeg" /><figcaption>Random code in the IDE :)</figcaption></figure><p>You have started working on a new project. And from time to time when you are reading the code in the repository you have this (hard to define) feeling. Feeling that something is wrong. So you are going deeper into the code and start playing with it.</p><p>Let’s have a look at the following piece of code that I have discovered. It was like this:</p><pre>router.get(&#39;/image/:url&#39;, (req, res) =&gt; {<br>  request<br>    .get(req.params.url)<br>    .pipe(res);<br>});</pre><p>This code had one job: download image(hosted on another website) and sent it to the user on our domain (<strong>myDomain.com</strong>).</p><p>For example, when you would go to the URL:<br><em>https://</em><strong><em>myDomain.com</em></strong><em>/image/https://</em><strong><em>another.domain</em></strong><em>/</em><strong><em>thepicture.jpg</em></strong><br>The users would see <strong>thepicture.jpg</strong> hosted on another.domain as hosted on your <strong>myDomain.com</strong> domain.</p><p>I should mention that: there are two assumptions: first is that request library only allows you to use HTTP and HTTPS links, and second: there is a caching layer between the code and client :)</p><p>The question arrives: What is wrong with this code? It is doing the job requested by the Product Owner / Client.</p><p>Yes. It does. It is doing even more. This code is script kiddies heaven (and for real bad actors too).</p><h3>Let see what vulnerabilities it opens.</h3><ul><li>You could put in the place of image parameter whatever you want. You could put your malicious script URL. Just prepare simple malicious HTML that contains some JS that (for example) would send cookies to your servers and then redirect users to for example image. Just to keep it quiet.<br>Now send your prepared URL to the users writing that the website is not working correctly. And they must check it! Before they become suspicious(if they ever would be) you will have their <strong>cookies</strong> and other data stored on <strong>myDomain.com</strong>.</li><li>Use this service as your’s files proxy — for serving viruses or similar things. Just imagine how your’s users are going to react when your website is going to be added to the blacklist with malicious domains.<br>That is what they are going to see:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*MUyU65xfWBXGGrZY.jpg" /></figure><ul><li>What is worst — you could put in the place of image param resources that are accessible only from your network — for example (intra.my.company/importantDoc.pdf). Files, documents, code — you name it. When the hacker is going to guess the local domain or IP address with data crucial to your business you have a data breach. And the GDPR on your back.</li></ul><h3>What could you do to prevent that or what should you do if you need to do that kind of a proxy?</h3><ul><li>Never allow wildcard redirects/content proxying. Always use some hash/checksum when generating URL. So users cannot put their (malicious) parameters.</li><li>Always check the type of content that you are downloading. If you want to proxy images — download only files that are of type image. And send header with content-type. The browser should follow the header value when trying to open the web address.</li><li>Isolate your servers from your corporate network. If you need to call some service in VPN — use non-standard port and/or proxy service that will monitor and allow for only a small number of types of requests.</li></ul><h3>Example of the code that should not have loopholes</h3><pre>const crypto = require(&#39;./crypto&#39;); // library that can encrypt and decrypt some text<br>const validator = require(&#39;./validator&#39;); // library that can perform validation on stream</pre><pre>const app = express();</pre><pre>const IMAGE_FROM_TRUSTED_SOURCE = &#39;<a href="https://trusted.domain/thepicture.jpg&#39;">https://trusted.domain/thepicture.jpg&#39;</a>;</pre><pre>router.get(&#39;/&#39;, (req, res) =&gt; {<br>  const {hash, cryptedUrl} = crypto.encrypt(IMAGE_FROM_TRUSTED_SOURCE);<br>  const imageUrl = `/image/v1/${hash}/${cryptedUrl}`;</pre><pre>res.end(`&lt;img src=&quot;${imageUrl}&quot; alt=&quot;Image that can be trusted&quot; /&gt;`);<br>});</pre><pre>router.get(&#39;/image/v1/:hash/:cryptedUrl&#39;, (req, res) =&gt; {<br>  const cryptedUrl = request.params.cryptedUrl;<br>  const hash = request.params.hash;</pre><pre>try {<br>    const url = crypto.decrypt(cryptedUrl, hash); // crypto has it&#39;s own secret/salt that is combined with &quot;public&quot; secret(hash)</pre><pre>request<br>      .get(url)<br>      .pipe(validator.validateContentType([&#39;image/jpg&#39;, &#39;image/png&#39;]))<br>      .pipe(res);<br>  } catch (error) {<br>    console.error(&#39;Someone is probably doing something naughty&#39;, error);</pre><pre>res.end();<br>  }<br>});</pre><p>As we can see when we want to send an image link to the user firstly we must encrypt the URL. It should be done by the commonly used library — for example, <a href="https://nodejs.org/api/crypto.html">Node.JS has Crypto</a>.</p><p>When the user wants to open your URL no malicious operation could be performed on URL because later the decryption is going to fail. The encryption itself should use besides key that is visible to the user(called in the code hash) also a private secret key that will never be known to the public.</p><p>So now we are safe and sound!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1836648eb2af" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>