about summary refs log tree commit diff stats
path: root/org.fox.ttrss/src/test/java/org/fox/ttrss/LoginResponseTest.java
blob: b06d692d6f3a3e707a33acdf138045cf9e79f608 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package org.fox.ttrss;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static org.junit.Assert.*;

/**
 * Tests for parsing TT-RSS API v2 login responses.
 * Uses fixture from src/test/resources/responses/login.json
 */
public class LoginResponseTest {

    private Gson gson;

    @Before
    public void setUp() {
        gson = new Gson();
    }

    private String loadFixture(String path) throws IOException {
        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new IOException("Fixture not found: " + path);
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        }
    }

    @Test
    public void loginResponse_hasSuccessTrue() throws IOException {
        String json = loadFixture("responses/login.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();

        assertTrue("Login response should have success=true",
                response.get("success").getAsBoolean());
    }

    @Test
    public void loginResponse_hasDataObject() throws IOException {
        String json = loadFixture("responses/login.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();

        assertNotNull("Login response should have data", response.has("data"));
        assertTrue("Login data should be an object", response.get("data").isJsonObject());
    }

    @Test
    public void loginResponse_hasSessionId() throws IOException {
        String json = loadFixture("responses/login.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();
        JsonObject data = response.getAsJsonObject("data");

        assertTrue("Login data should have session_id", data.has("session_id"));
        String sessionId = data.get("session_id").getAsString();
        assertFalse("Session ID should not be empty", sessionId.isEmpty());
    }

    @Test
    public void loginResponse_hasApiLevel() throws IOException {
        String json = loadFixture("responses/login.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();
        JsonObject data = response.getAsJsonObject("data");

        assertTrue("Login data should have api_level", data.has("api_level"));
        int apiLevel = data.get("api_level").getAsInt();
        assertTrue("API level should be positive", apiLevel > 0);
    }

    @Test
    public void loginResponse_hasConfig() throws IOException {
        String json = loadFixture("responses/login.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();
        JsonObject data = response.getAsJsonObject("data");
        JsonObject config = data.getAsJsonObject("config");

        assertNotNull("Login data should have config", config);
        assertTrue("Config should have icons_url", config.has("icons_url"));
        assertTrue("Config should have num_feeds", config.has("num_feeds"));
    }

    @Test
    public void loginResponse_errorIsNullOnSuccess() throws IOException {
        String json = loadFixture("responses/login.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();

        // error may be present but must be null on success
        JsonElement error = response.get("error");
        if (error != null) {
            assertTrue("Login response error should be null on success",
                    error.isJsonNull());
        }
    }

    @Test
    public void loginError_response_hasSuccessFalse() throws IOException {
        String json = loadFixture("responses/login_error.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();

        assertFalse("Failed login should have success=false",
                response.get("success").getAsBoolean());
    }

    @Test
    public void loginError_response_hasErrorObject() throws IOException {
        String json = loadFixture("responses/login_error.json");
        JsonObject response = JsonParser.parseString(json).getAsJsonObject();

        assertTrue("Failed login should have error", response.has("error"));
        JsonObject error = response.getAsJsonObject("error");
        assertEquals("Error code should be LOGIN_ERROR",
                "LOGIN_ERROR", error.get("code").getAsString());
    }
}