CA: warn if no access token present

Change-Id: Iec2e57edc7bb62bfed23672ada549252ba03518e
diff --git a/R/KorAPConnection.R b/R/KorAPConnection.R
index 9c5a141..83bfd5e 100644
--- a/R/KorAPConnection.R
+++ b/R/KorAPConnection.R
@@ -135,6 +135,20 @@
     NULL
 }
 
+
+warnIfNoAccessToken <- function(kco) {
+  if (is.null(kco@accessToken)) {
+    warning(
+      paste0(
+        "In order to receive KWICSs also from corpora with restricted licenses, you need an access token.\n",
+        "To generate an access token, login to KorAP and navigite to KorAP's OAuth settings <",
+        kco@KorAPUrl,
+        "settings/oauth#page-top>"
+      )
+    )
+  }
+}
+
 KorAPCacheSubDir <- function() {
   paste0("RKorAPClient_",
          gsub(
diff --git a/R/collocationAnalysis.R b/R/collocationAnalysis.R
index 2443f8f..b6592c3 100644
--- a/R/collocationAnalysis.R
+++ b/R/collocationAnalysis.R
@@ -85,6 +85,8 @@
               stop(sprintf("Not empty withinSpan (='%s') requires exactFrequencies=TRUE", withinSpan), call. = FALSE)
             }
 
+            warnIfNoAccessToken(kco)
+
             if (lemmatizeNodeQuery) {
               node <- lemmatizeWordQuery(node)
             }
@@ -314,5 +316,3 @@
         filter(frequency >= minOccur)
     }
   }
-
-
diff --git a/tests/testthat/test-collocations.R b/tests/testthat/test-collocations.R
index 55eed12..b97424b 100644
--- a/tests/testthat/test-collocations.R
+++ b/tests/testthat/test-collocations.R
@@ -9,27 +9,34 @@
   expect_equal(df$logDice, logDice(df$O1, df$O2, df$O, df$N, df$E, df$w))
 })
 
-test_that("collocationAnalysis works", {
-  kco <- new("KorAPConnection",
-             accessToken = NULL,
-             verbose = TRUE)
-  df <-
-    collocationAnalysis(
-      kco,
-      "focus([tt/p=ADJA] {Newstickeritis})",
-      vc = "corpusSigle=/W.D17/",
-      leftContextSize = 1,
-      rightContextSize = 0,
-      searchHitsSampleLimit = 100,
-      topCollocatesLimit = 1,
-      exactFrequencies = FALSE
+
+test_that("collocationAnalysis works and warns about missing token", {
+    kco <- new("KorAPConnection",
+               accessToken = NULL,
+               verbose = TRUE)
+    expect_warning(
+      df <-
+        collocationAnalysis(
+          kco,
+          "focus([tt/p=ADJA] {Newstickeritis})",
+          vc = "corpusSigle=/W.D17/",
+          leftContextSize = 1,
+          rightContextSize = 0,
+          searchHitsSampleLimit = 100,
+          topCollocatesLimit = 1,
+          exactFrequencies = FALSE
+        ),
+      "access token"
     )
   expect_gt(df$O, df$E)
   expect_gt(df$logDice, 1)
 })
 
 test_that("collocationAnalysis on unaccounted strings does not error out", {
-  kco <- new("KorAPConnection", cache = TRUE, verbose = TRUE)
-  df <- collocationAnalysis(kco, "XXXXXXXXAmeisenplage")
+  kco <- new("KorAPConnection", accessToken = NULL, verbose = TRUE)
+  expect_warning(
+    df <- collocationAnalysis(kco, "XXXXXXXXAmeisenplage"),
+    "access token"
+  )
   testthat::expect_equal(nrow(df), 0)
 })