-
Notifications
You must be signed in to change notification settings - Fork 14
vcr
rtoot
uses vcr for testing the parts that communicate with the Mastodon API. If you just need to run those tests, you don't need to do anything.
Let's assume you are a developer who needs to produce new tests (or "cassettes", in vcr's parlance). A cassette is a YAML file containing both the header and the response in the tests/fixtures
directory. This is an example of a cassette.
In order to prevent you from leaking any sensitive information:
You need to create the environment variable RTOOT_DEFAULT_TOKEN
to match the current token you are using. You can create this using the function convert_token_to_envvar
. This step is needed to censor your token in the cassette. It is because your token will be etched into the headers. We have instructed vcr
to censor it by matching RTOOT_DEFAULT_TOKEN
. If you set it up correctly, you should see the cassette file with a line like this instead of your actual token.
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer <<<MASTODON TOKEN>>>
It is better to put the environment variable in somewhere semi-permanent but with no risk of leaking, e.g. ~/.Renviron
. If you aren't sure about how to create the environment variable, contact us.
We will check the new cassettes for this kind of leakage. But please be responsible with your own secret!
After you have done the above, you can create a unit test like you usually do. You might consult the vcr
documentation on how to create a cassette. If you are lazy, just use the source, luke. When you run the test for the first time, it will communicate with the Mastodon API and then a "cassette" will be recorded. The next time you run the test, it will just "replay" from the cassette rather than communicate with the Mastodon API.
However, there is one important warning: Don't create cassettes with token = NULL
(the default)!
For some API calls, it would be okay, especially when instance
is specified and anonymous
is TRUE
. However, due to the decentralised nature of Mastodon, different people's token objects can point to different instances. And faux pas likes this would happen.
As you should have the environment variable RTOOT_DEFAULT_TOKEN
, please take the "fake token" approach. The instance doesn't need to be "social.tchncs.de", but you must pin down the instance used to create the cassette like so:
fake_token <- rtoot:::get_token_from_envvar("RTOOT_DEFAULT_TOKEN", check_stop = FALSE)
fake_token$type <- "user"
fake_token$instance <- "social.tchncs.de"
test_that("get_timeline_public", {
vcr::use_cassette("get_timeline_public_default", {
x <- get_timeline_public(token = fake_token)
})
})