<?xml version="1.0" encoding="utf-8" ?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:tt="http://teletype.in/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"><title>Pavel</title><author><name>Pavel</name></author><id>https://teletype.in/atom/kekocola</id><link rel="self" type="application/atom+xml" href="https://teletype.in/atom/kekocola?offset=0"></link><link rel="alternate" type="text/html" href="https://teletype.in/@kekocola?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=kekocola"></link><link rel="next" type="application/rss+xml" href="https://teletype.in/atom/kekocola?offset=10"></link><link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></link><updated>2026-04-19T11:55:57.487Z</updated><entry><id>kekocola:hBPzzPZqVQh</id><link rel="alternate" type="text/html" href="https://teletype.in/@kekocola/hBPzzPZqVQh?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=kekocola"></link><title>тестеры записываемся на копайлот</title><published>2024-02-28T18:31:51.314Z</published><updated>2024-02-28T18:52:07.672Z</updated><summary type="html">Идете и даете своему бекендеру пизды, чтобы он замутил свагер</summary><content type="html">
  &lt;h3 id=&quot;L1rC&quot;&gt;Первый шаг:&lt;/h3&gt;
  &lt;p id=&quot;OHL6&quot;&gt;Идете и даете своему бекендеру пизды, чтобы он замутил свагер&lt;/p&gt;
  &lt;h3 id=&quot;nx56&quot;&gt;Второй шаг:&lt;/h3&gt;
  &lt;p id=&quot;8eW6&quot;&gt;Копируете курл из сваггера и типовые ответы на запрос&lt;/p&gt;
  &lt;h3 id=&quot;3jaG&quot;&gt;Третий шаг:&lt;/h3&gt;
  &lt;p id=&quot;lOU0&quot;&gt;Идете в чатгпт или копайлот и делаете вот примерно такой запрос&lt;/p&gt;
  &lt;blockquote id=&quot;nmhU&quot;&gt;Hi. I have a curl reqest. Its a login request to my service and i want to make autotests for this request. Write tests for me. Use Java language and okhttp3 library. Each test should be a separate method.&lt;br /&gt;&lt;br /&gt;This is curl: &lt;code&gt;curl -X POST --header &amp;#x27;Content-Type: application/json&amp;#x27; --header &amp;#x27;Accept: application/json&amp;#x27; -d &amp;#x27;{ \ &amp;quot;email&amp;quot;: &amp;quot;m%40m.ru&amp;quot;, \ &amp;quot;password&amp;quot;: &amp;quot;password&amp;quot; \ }&amp;#x27; &amp;#x27;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;#x27;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;Threre is a 200 response, that comes if user go with correct credetensials. Token changes each time as user logins: &lt;code&gt;{ &amp;quot;refresh_token&amp;quot;: ИМАДЖИНИРУЙ ЧТО ТУТ ТОКЕН&amp;quot;, &amp;quot;token&amp;quot;: &amp;quot;ИМАДЖИНИРУЙ ЧТО ТУТ ТОКЕН&amp;quot; }&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;Threre is a 401 response if user go with invalid credetentials: &lt;code&gt;{ &amp;quot;status&amp;quot;: 401, &amp;quot;data&amp;quot;: { &amp;quot;email&amp;quot;: &amp;quot;donotoreply@.org&amp;quot; }, &amp;quot;error&amp;quot;: 2, &amp;quot;message&amp;quot;: &amp;quot;Wrong email or password&amp;quot; }&lt;/code&gt;&lt;/blockquote&gt;
  &lt;h3 id=&quot;Imkh&quot;&gt;Четвертый шаг:&lt;/h3&gt;
  &lt;p id=&quot;zdnd&quot;&gt;Ждете пока чудо Омниссии думает и выдает нам примерно вот что&lt;/p&gt;
  &lt;pre id=&quot;KYRz&quot; data-lang=&quot;java&quot;&gt;import okhttp3.*;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class LoginServiceTest {
    private static final MediaType JSON = MediaType.get(&amp;quot;application/json; charset=utf-8&amp;quot;);
    private static final OkHttpClient client = new OkHttpClient();

    private String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    @Test
    public void testSuccessfulLogin() throws IOException {
        String json = &amp;quot;{ \&amp;quot;email\&amp;quot;: \&amp;quot;m@m.ru\&amp;quot;, \&amp;quot;password\&amp;quot;: \&amp;quot;password\&amp;quot; }&amp;quot;;
        String response = post(&amp;quot;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;quot;, json);
        // Assert that the response contains a token
        assert(response.contains(&amp;quot;token&amp;quot;));
    }

    @Test
    public void testUnsuccessfulLogin() throws IOException {
        String json = &amp;quot;{ \&amp;quot;email\&amp;quot;: \&amp;quot;wrong@m.ru\&amp;quot;, \&amp;quot;password\&amp;quot;: \&amp;quot;wrongpassword\&amp;quot; }&amp;quot;;
        String response = post(&amp;quot;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;quot;, json);
        // Assert that the response contains an error message
        assertEquals(&amp;quot;{\&amp;quot;status\&amp;quot;:401,\&amp;quot;data\&amp;quot;:{\&amp;quot;email\&amp;quot;:\&amp;quot;donotoreply@.org\&amp;quot;},\&amp;quot;error\&amp;quot;:2,\&amp;quot;message\&amp;quot;:\&amp;quot;Wrong email or password\&amp;quot;}&amp;quot;, response);
    }
}
&lt;/pre&gt;
  &lt;h3 id=&quot;Q8TI&quot;&gt;Пятый шаг:&lt;/h3&gt;
  &lt;p id=&quot;ztOI&quot;&gt;Подумать самому все же придется, смотрим что тут не так. Хуемое, джейсончики как стринги прямо в коде, это беспонтово. Еще можно урл объявлять прям в самом начале, тестим то токо один метод.&lt;/p&gt;
  &lt;p id=&quot;dhhK&quot;&gt;Хуярим новый промт&lt;/p&gt;
  &lt;blockquote id=&quot;a5TE&quot;&gt;Can you fix what you write please. Request url &lt;code&gt;ИМАДЖИНИРУЙ ТУТ ЮРЛ&lt;/code&gt; should not be specified in tests it should be specified as a variable in the begining of class.&lt;br /&gt;&lt;br /&gt;Also please make request body as separate class with getters and setters&lt;/blockquote&gt;
  &lt;h3 id=&quot;zWRK&quot;&gt;Шестой шаг:&lt;/h3&gt;
  &lt;p id=&quot;r5b2&quot;&gt;Оно нам нахуевертит вот такого примерно&lt;/p&gt;
  &lt;pre id=&quot;JJKC&quot; data-lang=&quot;java&quot;&gt;import okhttp3.*;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class LoginServiceTest {
    private static final MediaType JSON = MediaType.get(&amp;quot;application/json; charset=utf-8&amp;quot;);
    private static final OkHttpClient client = new OkHttpClient();
    private static final String URL = &amp;quot;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;quot;;
    private static final Gson gson = new Gson();

    private String post(String url, LoginBody body) throws IOException {
        RequestBody requestBody = RequestBody.create(gson.toJson(body), JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    @Test
    public void testSuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;m@m.ru&amp;quot;, &amp;quot;password&amp;quot;);
        String response = post(URL, body);
        // Assert that the response contains a token
        assert(response.contains(&amp;quot;token&amp;quot;));
    }

    @Test
    public void testUnsuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;wrong@m.ru&amp;quot;, &amp;quot;wrongpassword&amp;quot;);
        String response = post(URL, body);
        // Assert that the response contains an error message
        assertEquals(&amp;quot;{\&amp;quot;status\&amp;quot;:401,\&amp;quot;data\&amp;quot;:{\&amp;quot;email\&amp;quot;:\&amp;quot;donotoreply@.org\&amp;quot;},\&amp;quot;error\&amp;quot;:2,\&amp;quot;message\&amp;quot;:\&amp;quot;Wrong email or password\&amp;quot;}&amp;quot;, response);
    }

    static class LoginBody {
        private String email;
        private String password;

        public LoginBody(String email, String password) {
            this.email = email;
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}&lt;/pre&gt;
  &lt;h3 id=&quot;wQW0&quot;&gt;Седьмой шаг:&lt;/h3&gt;
  &lt;p id=&quot;1JQc&quot;&gt;Думаем по новой, вспоминаем, что есть плагин который сам ставит геттеры сеттеры. Пишем.&lt;/p&gt;
  &lt;blockquote id=&quot;pFU1&quot;&gt;Please use Lombok annotations and change LoginBody class&lt;/blockquote&gt;
  &lt;h3 id=&quot;zje2&quot;&gt;Восьмой шаг:&lt;/h3&gt;
  &lt;p id=&quot;rctC&quot;&gt;Воооо, теперь почти заебись&lt;/p&gt;
  &lt;pre id=&quot;H9yY&quot; data-lang=&quot;java&quot;&gt;
import okhttp3.*;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import lombok.Getter;
import lombok.Setter;
import lombok.AllArgsConstructor;

public class LoginServiceTest {
    private static final MediaType JSON = MediaType.get(&amp;quot;application/json; charset=utf-8&amp;quot;);
    private static final OkHttpClient client = new OkHttpClient();
    private static final String URL = &amp;quot;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;quot;;
    private static final Gson gson = new Gson();

    private String post(String url, LoginBody body) throws IOException {
        RequestBody requestBody = RequestBody.create(gson.toJson(body), JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    @Test
    public void testSuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;m@m.ru&amp;quot;, &amp;quot;password&amp;quot;);
        String response = post(URL, body);
        // Assert that the response contains a token
        assert(response.contains(&amp;quot;token&amp;quot;));
    }

    @Test
    public void testUnsuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;wrong@m.ru&amp;quot;, &amp;quot;wrongpassword&amp;quot;);
        String response = post(URL, body);
        // Assert that the response contains an error message
        assertEquals(&amp;quot;{\&amp;quot;status\&amp;quot;:401,\&amp;quot;data\&amp;quot;:{\&amp;quot;email\&amp;quot;:\&amp;quot;donotoreply@.org\&amp;quot;},\&amp;quot;error\&amp;quot;:2,\&amp;quot;message\&amp;quot;:\&amp;quot;Wrong email or password\&amp;quot;}&amp;quot;, response);
    }

    @Getter
    @Setter
    @AllArgsConstructor
    static class LoginBody {
        private String email;
        private String password;
    }
}&lt;/pre&gt;
  &lt;h3 id=&quot;jxok&quot;&gt;Девятый шаг:&lt;/h3&gt;
  &lt;p id=&quot;dWX2&quot;&gt;Бля, забыли про респонс боди, вежливо просим поправить.&lt;/p&gt;
  &lt;blockquote id=&quot;H2Zy&quot;&gt;Thank you. Now please make a response body as sepatate class&lt;/blockquote&gt;
  &lt;h3 id=&quot;4xKE&quot;&gt;Десятый шаг:&lt;/h3&gt;
  &lt;p id=&quot;TAiO&quot;&gt;Смотрим, вроде заебись&lt;/p&gt;
  &lt;pre id=&quot;ytQt&quot; data-lang=&quot;java&quot;&gt;import okhttp3.*;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import lombok.Getter;
import lombok.Setter;
import lombok.AllArgsConstructor;

public class LoginServiceTest {
    private static final MediaType JSON = MediaType.get(&amp;quot;application/json; charset=utf-8&amp;quot;);
    private static final OkHttpClient client = new OkHttpClient();
    private static final String URL = &amp;quot;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;quot;;
    private static final Gson gson = new Gson();

    private ResponseBody post(String url, LoginBody body) throws IOException {
        RequestBody requestBody = RequestBody.create(gson.toJson(body), JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return gson.fromJson(response.body().string(), ResponseBody.class);
        }
    }

    @Test
    public void testSuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;m@m.ru&amp;quot;, &amp;quot;password&amp;quot;);
        ResponseBody response = post(URL, body);
        // Assert that the response contains a token
        assert(response.getToken() != null);
    }

    @Test
    public void testUnsuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;wrong@m.ru&amp;quot;, &amp;quot;wrongpassword&amp;quot;);
        ResponseBody response = post(URL, body);
        // Assert that the response contains an error message
        assertEquals(&amp;quot;Wrong email or password&amp;quot;, response.getMessage());
    }

    @Getter
    @Setter
    @AllArgsConstructor
    static class LoginBody {
        private String email;
        private String password;
    }

    @Getter
    @Setter
    static class ResponseBody {
        private String token;
        private String refresh_token;
        private int status;
        private Data data;
        private int error;
        private String message;

        @Getter
        @Setter
        static class Data {
            private String email;
        }
    }
}&lt;/pre&gt;
  &lt;h3 id=&quot;pNXw&quot;&gt;Одиннадцатый шаг:&lt;/h3&gt;
  &lt;p id=&quot;epQI&quot;&gt;Так, ну ладно, пора поработать руками. Сразу видим какую-то залупу с респос боди, некрасиво, но нам похуй, работает же.&lt;br /&gt;&lt;br /&gt;Копируем весь код в IDEA, сразу видим что все красное и ничо не работает, нажимаем пару раз Cntrl+Enter, ага, неправильно библиотеки подтянули. Кликаем cntrl+enter пока большинство проблем не решится. Остальное правим руками.&lt;/p&gt;
  &lt;h3 id=&quot;afvi&quot;&gt;Итого что вышло по коду:&lt;/h3&gt;
  &lt;pre id=&quot;0FRj&quot; data-lang=&quot;java&quot;&gt;package http;

import okhttp3.*;
import org.junit.Test;
import com.google.gson.Gson;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import lombok.Getter;
import lombok.Setter;
import lombok.AllArgsConstructor;

public class LoginServiceTest {
    private static final MediaType JSON = MediaType.get(&amp;quot;application/json; charset=utf-8&amp;quot;);
    private static final OkHttpClient client = new OkHttpClient();
    private static final String URL = &amp;quot;ИМАДЖИНИРУЙ ТУТ ЮРЛ&amp;quot;;
    private static final Gson gson = new Gson();

    private ResponseBody post(String url, LoginBody body) throws IOException {
        RequestBody requestBody = RequestBody.create(gson.toJson(body), JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return gson.fromJson(response.body().string(), ResponseBody.class);
        }
    }

    @Test
    public void testSuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;m@m.ru&amp;quot;, &amp;quot;password&amp;quot;);
        ResponseBody response = post(URL, body);
        // Assert that the response contains a token
        assert(response.getToken() != null);
    }

    @Test
    public void testUnsuccessfulLogin() throws IOException {
        LoginBody body = new LoginBody(&amp;quot;wrong@m.ru&amp;quot;, &amp;quot;wrongpassword&amp;quot;);
        ResponseBody response = post(URL, body);
        // Assert that the response contains an error message
        assertEquals(&amp;quot;Wrong email or password&amp;quot;, response.getMessage());
    }

    @Getter
    @Setter
    @AllArgsConstructor
    static class LoginBody {
        private String email;
        private String password;
    }

    @Getter
    @Setter
    static class ResponseBody {
        private String token;
        private String refresh_token;
        private int status;
        private Data data;
        private int error;
        private String message;

        @Getter
        @Setter
        static class Data {
            private String email;
        }
    }
}
&lt;/pre&gt;

</content></entry></feed>