blob: 9a52c4648284490e085236bd68d9f0ff93ea0be8 [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")
Marc Kupietzbd9796f2025-06-26 15:40:08 +020057
58 # Test keyring functionality - skip if keyring setup fails
59 keyring_available <- tryCatch({
60 # Try to access keyring backend
61 keyring::default_backend()
62 TRUE
63 }, error = function(e) {
64 FALSE
65 })
66
67 skip_if(!keyring_available, "Keyring not properly configured for testing")
68
Marc Kupietz7de5f322025-06-04 17:17:22 +020069 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
70 test_token <- "test_access_token_123"
71
72 # Test that persistAccessToken function exists and is callable
73 expect_true(is.function(persistAccessToken))
74
75 # Test that we can call the function with a token
Marc Kupietz7de5f322025-06-04 17:17:22 +020076 expect_error(persistAccessToken(kco, accessToken = test_token), NA)
77})
78
79test_that("persistAccessToken warns about OAuth client tokens", {
80 skip_if_not_installed("keyring")
81 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
82 # Simulate OAuth client
83 kco@oauthClient <- list(id = "test")
84
85 expect_warning(persistAccessToken(kco), "Short lived access tokens.*cannot be persisted")
86})
87
88test_that("clearAccessToken removes token", {
89 skip_if_not_installed("keyring")
Marc Kupietzbd9796f2025-06-26 15:40:08 +020090
91 # Test keyring functionality - skip if keyring setup fails
92 keyring_available <- tryCatch({
93 # Try to access keyring backend
94 keyring::default_backend()
95 TRUE
96 }, error = function(e) {
97 FALSE
98 })
99
100 skip_if(!keyring_available, "Keyring not properly configured for testing")
101
Marc Kupietz7de5f322025-06-04 17:17:22 +0200102 kco <- KorAPConnection(accessToken = "test_token", timeout = 1)
103
104 # Test that clearAccessToken function exists and is callable
105 expect_true(is.function(clearAccessToken))
106
107 # Test that we can call the function
108 result <- clearAccessToken(kco)
109 expect_true(is(result, "KorAPConnection"))
110})
111
112test_that("clearAccessToken handles keyring errors gracefully", {
113 skip_if_not_installed("keyring")
114 kco <- KorAPConnection(accessToken = "test_token", timeout = 1)
115
116 # Test that clearAccessToken doesn't crash when keyring operations fail
117 # We'll just test that the function exists and is callable
118 expect_true(is.function(clearAccessToken))
119})
120
121test_that("getAccessToken retrieves token from keyring", {
122 skip_if_not_installed("keyring")
123
124 # Test that getAccessToken function exists and handles missing keys gracefully
125 expect_true(is.function(RKorAPClient:::getAccessToken))
126
127 # Test with a non-existent service - should return NULL gracefully
128 result <- RKorAPClient:::getAccessToken("non-existent-service")
129 expect_true(is.null(result) || is.character(result))
130})
131
132test_that("getAccessToken returns NULL when token not found", {
133 skip_if_not_installed("keyring")
134
135 # Test that getAccessToken handles missing tokens gracefully
136 result <- RKorAPClient:::getAccessToken("definitely-non-existent-service")
137 expect_true(is.null(result) || is.character(result))
138})
139
140test_that("getAccessToken handles keyring errors gracefully", {
141 skip_if_not_installed("keyring")
142
143 # Test that getAccessToken function exists and handles errors gracefully
144 expect_true(is.function(RKorAPClient:::getAccessToken))
145
146 # Test with a service that likely doesn't exist
147 result <- RKorAPClient:::getAccessToken("non-existent-keyring-service")
148 expect_true(is.null(result) || is.character(result))
149})
150
151test_that("warnIfNotAuthorized issues warning when needed", {
152 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
153 kco@authorizationSupported <- TRUE
154 kco@accessToken <- NULL
155 kco@oauthClient <- NULL
156
157 expect_warning(RKorAPClient:::warnIfNotAuthorized(kco), "authorize your application")
158})
159
160test_that("warnIfNotAuthorized does not warn when authorized", {
161 kco <- KorAPConnection(accessToken = "test_token", timeout = 1)
162 kco@authorizationSupported <- TRUE
163
164 expect_silent(RKorAPClient:::warnIfNotAuthorized(kco))
165})
166
167test_that("warnIfNotAuthorized does not warn when authorization not supported", {
168 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
169 kco@authorizationSupported <- FALSE
170
171 expect_silent(RKorAPClient:::warnIfNotAuthorized(kco))
172})
173
174test_that("KorAPCacheSubDir returns correct directory name", {
175 cache_dir <- RKorAPClient:::KorAPCacheSubDir()
176 expect_true(grepl("^RKorAPClient_[0-9]+\\.[0-9]+$", cache_dir))
177})
178
179test_that("clearCache clears the cache directory", {
180 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
181
182 # Test that clearCache function exists and is callable
183 expect_true(is.function(clearCache))
184
185 # Test that clearCache doesn't error
186 expect_error(clearCache(kco), NA)
187})
188
189test_that("auth method handles unsupported authorization", {
190 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
191 kco@authorizationSupported <- FALSE
192
193 result <- auth(kco)
194 expect_identical(result, kco)
195})
196
197test_that("auth method warns about wrong instance for default app_id", {
198 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
199 kco@authorizationSupported <- TRUE
200 kco@KorAPUrl <- "https://other.instance.de/"
201
202 expect_warning(auth(kco), "You can use the default app_id only for")
203})
204
205test_that("apiCall handles no internet connection", {
206 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
207
208 # Test that apiCall function exists and is callable
209 expect_true(is.function(apiCall))
210
211 # Test with an invalid URL that should fail gracefully
212 expect_message(result <- apiCall(kco, "http://definitely-invalid-url-12345.com"),
213 "No internet|Error|failed|resolve|timeout",
214 ignore.case = TRUE
215 )
216})
217
218test_that("apiCall handles timeout correctly", {
219 kco <- KorAPConnection(accessToken = NULL, timeout = 0.001)
220
221 # Test with a very short timeout and a slow endpoint
222 expect_message(result <- apiCall(kco, "http://httpbin.org/delay/2"),
223 "Error:|Timeout|failed",
224 ignore.case = TRUE
225 )
226})
227
228test_that("apiCall handles HTTP error status codes", {
229 skip_if_offline()
230 kco <- KorAPConnection(accessToken = NULL, timeout = 3)
231
232 # Test with an endpoint that returns 404
233 expect_message(result <- apiCall(kco, "http://httpbin.org/status/404"),
234 "Error.*404|failed|request",
235 ignore.case = TRUE
236 )
237})
238
239test_that("apiCall returns cached results when available", {
240 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, timeout = 1)
241
242 # Test that apiCall works with cache enabled
243 expect_true(is.function(apiCall))
244 expect_true(kco@cache)
245
246 # The specific caching logic is tested indirectly through other tests
247 expect_true(TRUE)
248})
249
250test_that("apiCall handles JSON parsing errors", {
251 skip_if_offline()
252 kco <- KorAPConnection(accessToken = NULL, timeout = 3)
253
254 # Test with an endpoint that returns HTML instead of JSON
255 expect_message(result <- apiCall(kco, "http://httpbin.org/html", json = TRUE),
256 "API did not return JSON|Failed to parse|Error|html",
257 ignore.case = TRUE
258 )
259})
260
261test_that("apiCall handles warnings in response", {
262 skip_if_offline()
263 kco <- KorAPConnection(accessToken = NULL, timeout = 1)
264
265 # Create a mock response with warnings for testing
266 mock_response <- list(warnings = data.frame(code = "682", message = "test warning"))
267
268 # Test that the warning handling logic exists
269 expect_true(is.list(mock_response))
270 expect_true("warnings" %in% names(mock_response))
271})
272
273test_that("apiCall saves to cache on successful response", {
274 kco <- KorAPConnection(accessToken = NULL, cache = TRUE, timeout = 1)
275
276 # Test that caching is enabled
277 expect_true(kco@cache)
278
279 # The actual caching behavior is tested through integration tests
280 expect_true(is.function(apiCall))
281})