Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/settings.test.ts (TypeScript)

Code before:
        });

        expect(response.body).toEqual({
          message: 'flash.updated-about-me',
          type: 'success'
        });
        expect(response.statusCode).toEqual(200);
      });

      test('PUT throw 500 if the image is invalid', async () => {
        const response = await superRequest('/update-my-about', {
          method: 'PUT',
          setCookies
        }).send({
          about: 'Teacher at freeCodeCamp',
          name: 'Quincy Larson',
          location: 'USA',
          picture: 'invaild'
        });

        expect(response.body).toEqual({
          message:
            'Failed to serialize an error. Error: "type" is required!. Original error: cb is not a function',
          code: 'FST_ERR_FAILED_ERROR_SERIALIZATION',
          statusCode: 500
        });
      });
    });

    describe('/update-my-honesty', () => {
      test('PUT returns 200 status code with "success" message', async () => {
        const response = await superRequest('/update-my-honesty', {
          method: 'PUT',
          setCookies
        }).send({ isHonest: true });

        expect(response?.statusCode).toEqual(200);

        expect(response?.body).toEqual({
          message: 'buttons.accepted-honesty',
          type: 'success'
        });
      });

      test('PUT returns 400 status code with invalid honesty', async () => {
        const response = await superRequest('/update-my-honesty', {
          method: 'PUT',
          setCookies
        }).send({ isHonest: false });

        expect(response?.statusCode).toEqual(400);

Review comment (suggestion):
We should never see these errors in practice, since it means that there's an error in our code. What should happen is that the response we send is valid according to the response schema.

So, in this case we should get a success response, because the current api simply ignores strings that are not urls.

Code after:
        const secondUpdate = await superRequest('/update-my-username', {
          method: 'PUT',
          setCookies
        }).send({
          username: 'twaha2'
        });

        expect(secondUpdate.body).toEqual({
          message: 'flash.username-used',
          type: 'info'
        });
        expect(secondUpdate.statusCode).toEqual(400);

        // Not allowed because, while the usernameDisplay is different, the
        // username is not
        const existingUser = await superRequest('/update-my-username', {
          method: 'PUT',
          setCookies
        }).send({
          username: 'SemBauke'
        });

        expect(existingUser.body).toEqual({
          message: 'flash.username-taken',
          type: 'info'
        });
        expect(existingUser.statusCode).toEqual(400);
      });

      test('PUT returns 200 status code with "success" message', async () => {
        await superRequest('/update-my-username', {
          method: 'PUT',
          setCookies
        }).send({
          username: 'twaha3'
        });

        const response = await superRequest('/update-my-username', {
          method: 'PUT',
          setCookies
        }).send({
          username: 'TWaha3'
        });

        expect(response?.body).toEqual({
          message: 'flash.username-updated',
          type: 'success',
          username: 'TWaha3'
        });
        expect(response?.statusCode).toEqual(200);
      });