blob: 3ee7bb74a8f6f172047bc1b123231714fbf4563c [file] [log] [blame]
Marc Kupietza4675722022-02-23 23:55:15 +01001test_that("KorAPConnection fails gracefully on unresolvable host", {
Marc Kupietzb1dec012025-06-04 17:16:57 +02002 expect_message(KorAPConnection(accessToken = NULL, apiUrl = "http://xxx.asdhsahdsadhvgas.org"), "No internet|Could not resolve")
Marc Kupietza4675722022-02-23 23:55:15 +01003})
4
5test_that("KorAPConnection fails gracefully on timeout", {
Marc Kupietzb1dec012025-06-04 17:16:57 +02006 expect_message(KorAPConnection(apiUrl = "http://httpbin.org/delay/3", accessToken = NULL, timeout = 0.2), "No internet|Timeout|json|progress|Unavailable")
Marc Kupietza4675722022-02-23 23:55:15 +01007})
8
Marc Kupietzf6506292023-04-18 07:06:54 +02009test_that("KorAPConnection fails gracefully on Bad Gateway errors", {
Marc Kupietzb1dec012025-06-04 17:16:57 +020010 expect_message(KorAPConnection(apiUrl = "http://httpbin.org/status/502", accessToken = NULL, timeout = 0.5), "No internet|Timeout|progress|json|502")
Marc Kupietzf6506292023-04-18 07:06:54 +020011})
12
Marc Kupietz5d9e3a22020-03-23 09:03:29 +010013test_that("KorAPConnection is printable", {
Marc Kupietz617266d2025-02-27 10:43:07 +010014 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
Marc Kupietza4675722022-02-23 23:55:15 +010015 skip_if(is.null(kco@welcome))
Marc Kupietz5d9e3a22020-03-23 09:03:29 +010016 expect_error(print(kco), NA)
17})
18
Marc Kupietzb2b32a32020-03-24 13:56:50 +010019test_that("Opening KorAPConnection prints some message.", {
Marc Kupietz617266d2025-02-27 10:43:07 +010020 expect_message(KorAPConnection(accessToken = NULL), "KorAP")
Marc Kupietzb2b32a32020-03-24 13:56:50 +010021})
22
Marc Kupietza4675722022-02-23 23:55:15 +010023test_that("Opening KorAPConnection with invalid apiToken fails gracefully", {
Marc Kupietzb1dec012025-06-04 17:16:57 +020024 expect_message(
25 KorAPConnection(accessToken = "test token", timeout = 3),
26 "401|Timeout|progress"
27 )
Marc Kupietzb2b32a32020-03-24 13:56:50 +010028})
29
30test_that("Persisting null apiToken fails", {
Marc Kupietz617266d2025-02-27 10:43:07 +010031 kco <- KorAPConnection(accessToken = NULL, timeout = 3)
Marc Kupietz5a61f012021-03-04 18:25:27 +010032 skip_if_not(is.null(kco@accessToken))
Marc Kupietza4675722022-02-23 23:55:15 +010033 skip_if(is.null(kco@welcome))
Marc Kupietz5d9e3a22020-03-23 09:03:29 +010034 expect_error(persistAccessToken(kco),
Marc Kupietzb1dec012025-06-04 17:16:57 +020035 ".*not supplied any access token.*",
36 perl = TRUE
37 )
Marc Kupietz5d9e3a22020-03-23 09:03:29 +010038})
39
40test_that("Opening KorAPConnection with KorAPUrl works", {
Marc Kupietzb1dec012025-06-04 17:16:57 +020041 kco <- KorAPConnection(accessToken = NULL, KorAPUrl = "https://korap.ids-mannheim.de", timeout = 1)
Marc Kupietz5d9e3a22020-03-23 09:03:29 +010042 expect_equal(kco@apiUrl, paste0("https://korap.ids-mannheim.de/api/", kco@apiVersion, "/"))
Marc Kupietzb1dec012025-06-04 17:16:57 +020043 kco <- KorAPConnection(accessToken = NULL, KorAPUrl = "https://korap.ids-mannheim.de/", timeout = 1)
Marc Kupietz5d9e3a22020-03-23 09:03:29 +010044 expect_equal(kco@apiUrl, paste0("https://korap.ids-mannheim.de/api/", kco@apiVersion, "/"))
45})
Marc Kupietz7de5f322025-06-04 17:17:22 +020046
47# New tests for improved coverage
48
49test_that("show method displays connection info correctly", {
50 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
51 expect_output(show(kco), "<KorAPConnection>")
52 expect_output(show(kco), "apiUrl:")
53})
54
55test_that("persistAccessToken works with valid token", {
56 skip_if_not_installed("keyring")
57 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
58 test_token <- "test_access_token_123"
59
60 # Test that persistAccessToken function exists and is callable
61 expect_true(is.function(persistAccessToken))
62
63 # Test that we can call the function with a token
64 # This will test the function logic without relying on keyring
65 expect_error(persistAccessToken(kco, accessToken = test_token), NA)
66})
67
68test_that("persistAccessToken warns about OAuth client tokens", {
69 skip_if_not_installed("keyring")
70 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
71 # Simulate OAuth client
72 kco@oauthClient <- list(id = "test")
73
74 expect_warning(persistAccessToken(kco), "Short lived access tokens.*cannot be persisted")
75})
76
77test_that("clearAccessToken removes token", {
78 skip_if_not_installed("keyring")
79 kco <- KorAPConnection(accessToken = "test_token", timeout = 1)
80
81 # Test that clearAccessToken function exists and is callable
82 expect_true(is.function(clearAccessToken))
83
84 # Test that we can call the function
85 result <- clearAccessToken(kco)
86 expect_true(is(result, "KorAPConnection"))
87})
88
89test_that("clearAccessToken handles keyring errors gracefully", {
90 skip_if_not_installed("keyring")
91 kco <- KorAPConnection(accessToken = "test_token", timeout = 1)
92
93 # Test that clearAccessToken doesn't crash when keyring operations fail
94 # We'll just test that the function exists and is callable
95 expect_true(is.function(clearAccessToken))
96})
97
98test_that("getAccessToken retrieves token from keyring", {
99 skip_if_not_installed("keyring")
100
101 # Test that getAccessToken function exists and handles missing keys gracefully
102 expect_true(is.function(RKorAPClient:::getAccessToken))
103
104 # Test with a non-existent service - should return NULL gracefully
105 result <- RKorAPClient:::getAccessToken("non-existent-service")
106 expect_true(is.null(result) || is.character(result))
107})
108
109test_that("getAccessToken returns NULL when token not found", {
110 skip_if_not_installed("keyring")
111
112 # Test that getAccessToken handles missing tokens gracefully
113 result <- RKorAPClient:::getAccessToken("definitely-non-existent-service")
114 expect_true(is.null(result) || is.character(result))
115})
116
117test_that("getAccessToken handles keyring errors gracefully", {
118 skip_if_not_installed("keyring")
119
120 # Test that getAccessToken function exists and handles errors gracefully
121 expect_true(is.function(RKorAPClient:::getAccessToken))
122
123 # Test with a service that likely doesn't exist
124 result <- RKorAPClient:::getAccessToken("non-existent-keyring-service")
125 expect_true(is.null(result) || is.character(result))
126})
127
128test_that("warnIfNotAuthorized issues warning when needed", {
129 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
130 kco@authorizationSupported <- TRUE
131 kco@accessToken <- NULL
132 kco@oauthClient <- NULL
133
134 expect_warning(RKorAPClient:::warnIfNotAuthorized(kco), "authorize your application")
135})
136
137test_that("warnIfNotAuthorized does not warn when authorized", {
138 kco <- KorAPConnection(accessToken = "test_token", timeout = 1)
139 kco@authorizationSupported <- TRUE
140
141 expect_silent(RKorAPClient:::warnIfNotAuthorized(kco))
142})
143
144test_that("warnIfNotAuthorized does not warn when authorization not supported", {
145 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
146 kco@authorizationSupported <- FALSE
147
148 expect_silent(RKorAPClient:::warnIfNotAuthorized(kco))
149})
150
151test_that("KorAPCacheSubDir returns correct directory name", {
152 cache_dir <- RKorAPClient:::KorAPCacheSubDir()
153 expect_true(grepl("^RKorAPClient_[0-9]+\\.[0-9]+$", cache_dir))
154})
155
156test_that("clearCache clears the cache directory", {
157 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
158
159 # Test that clearCache function exists and is callable
160 expect_true(is.function(clearCache))
161
162 # Test that clearCache doesn't error
163 expect_error(clearCache(kco), NA)
164})
165
166test_that("auth method handles unsupported authorization", {
167 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
168 kco@authorizationSupported <- FALSE
169
170 result <- auth(kco)
171 expect_identical(result, kco)
172})
173
174test_that("auth method warns about wrong instance for default app_id", {
175 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
176 kco@authorizationSupported <- TRUE
177 kco@KorAPUrl <- "https://other.instance.de/"
178
179 expect_warning(auth(kco), "You can use the default app_id only for")
180})
181
182test_that("apiCall handles no internet connection", {
183 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
184
185 # Test that apiCall function exists and is callable
186 expect_true(is.function(apiCall))
187
188 # Test with an invalid URL that should fail gracefully
189 expect_message(result <- apiCall(kco, "http://definitely-invalid-url-12345.com"),
190 "No internet|Error|failed|resolve|timeout",
191 ignore.case = TRUE
192 )
193})
194
195test_that("apiCall handles timeout correctly", {
196 kco <- KorAPConnection(accessToken = NULL, timeout = 0.001)
197
198 # Test with a very short timeout and a slow endpoint
199 expect_message(result <- apiCall(kco, "http://httpbin.org/delay/2"),
200 "Error:|Timeout|failed",
201 ignore.case = TRUE
202 )
203})
204
205test_that("apiCall handles HTTP error status codes", {
206 skip_if_offline()
207 kco <- KorAPConnection(accessToken = NULL, timeout = 3)
208
209 # Test with an endpoint that returns 404
210 expect_message(result <- apiCall(kco, "http://httpbin.org/status/404"),
211 "Error.*404|failed|request",
212 ignore.case = TRUE
213 )
214})
215
216test_that("apiCall returns cached results when available", {
217 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, timeout = 1)
218
219 # Test that apiCall works with cache enabled
220 expect_true(is.function(apiCall))
221 expect_true(kco@cache)
222
223 # The specific caching logic is tested indirectly through other tests
224 expect_true(TRUE)
225})
226
227test_that("apiCall handles JSON parsing errors", {
228 skip_if_offline()
229 kco <- KorAPConnection(accessToken = NULL, timeout = 3)
230
231 # Test with an endpoint that returns HTML instead of JSON
232 expect_message(result <- apiCall(kco, "http://httpbin.org/html", json = TRUE),
233 "API did not return JSON|Failed to parse|Error|html",
234 ignore.case = TRUE
235 )
236})
237
238test_that("apiCall handles warnings in response", {
239 skip_if_offline()
240 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
241
242 # Create a mock response with warnings for testing
243 mock_response <- list(warnings = data.frame(code = "682", message = "test warning"))
244
245 # Test that the warning handling logic exists
246 expect_true(is.list(mock_response))
247 expect_true("warnings" %in% names(mock_response))
248})
249
250test_that("apiCall saves to cache on successful response", {
251 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, timeout = 1)
252
253 # Test that caching is enabled
254 expect_true(kco@cache)
255
256 # The actual caching behavior is tested through integration tests
257 expect_true(is.function(apiCall))
258})