fix: use dotenv to load test data
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2024-01-02 20:10:06 +05:30
parent b15e9ebe18
commit 0f9caf7777
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 18 additions and 13 deletions

View file

@ -43,4 +43,5 @@ steps:
- PASSWORD=password - PASSWORD=password
- INSTANCE_URL=http://mcaptcha:7000 - INSTANCE_URL=http://mcaptcha:7000
commands: commands:
- . .env_sample && make test - mv .env_sample .env.local
- make test

View file

@ -12,3 +12,4 @@ url = "2.5.0"
[dev-dependencies] [dev-dependencies]
actix-rt = "2.9.0" actix-rt = "2.9.0"
dotenv = "0.15.0"

View file

@ -9,16 +9,13 @@ test.env:
$(eval WIDGET_URL = $(shell . ./venv/bin/activate && python ./scripts/create_captcha.py widget_url $(INSTANCE_URL) $(SITEKEY))) $(eval WIDGET_URL = $(shell . ./venv/bin/activate && python ./scripts/create_captcha.py widget_url $(INSTANCE_URL) $(SITEKEY)))
$(eval TOKEN = $(shell ./scripts/download-cli.sh gen_pow $(WIDGET_URL))) $(eval TOKEN = $(shell ./scripts/download-cli.sh gen_pow $(WIDGET_URL)))
@-rm .env.local @-rm .env.local
echo "export SECRET=$(SECRET)" > .env.local echo "SECRET=$(SECRET)" > .env.local
echo "export SITEKEY=$(SITEKEY)" >> .env.local echo "SITEKEY=$(SITEKEY)" >> .env.local
echo "export WIDGET_URL=$(WIDGET_URL)" >> .env.local echo "TOKEN=$(TOKEN)" >> .env.local
echo "export TOKEN=$(TOKEN)" >> .env.local
test: test:
SITEKEY=$(SITEKEY) SECRET=$(SECRET) \ cargo test
INSTANCE_URL=$(INSTANCE_URL) \
TOKEN=$(TOKEN) cargo test
lint: lint:
black src/ scripts/ black src/ scripts/

View file

@ -28,11 +28,9 @@ impl MCaptcha {
.post(self.verify_path.clone()) .post(self.verify_path.clone())
.json(&payload) .json(&payload)
.send() .send()
.await .await?
.unwrap()
.json() .json()
.await .await?;
.unwrap();
Ok(res.valid) Ok(res.valid)
} }
@ -62,16 +60,24 @@ impl<'a> CaptchaVerfiyPayload<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use std::env; use std::env;
use super::*;
#[actix_rt::test] #[actix_rt::test]
async fn verify_works() { async fn verify_works() {
dotenv::from_filename(".env.local").ok();
let instance_url = env::var("INSTANCE_URL").expect("instance url not set"); let instance_url = env::var("INSTANCE_URL").expect("instance url not set");
let sitekey = env::var("SITEKEY").expect("sitekey not set"); let sitekey = env::var("SITEKEY").expect("sitekey not set");
let secret = env::var("SECRET").expect("secret not set"); let secret = env::var("SECRET").expect("secret not set");
let token = env::var("TOKEN").expect("token not set"); let token = env::var("TOKEN").expect("token not set");
println!("token={token}");
println!("sitekey={sitekey}");
println!("secret={secret}");
println!("instance_url={instance_url}");
let mcaptcha = MCaptcha::new( let mcaptcha = MCaptcha::new(
sitekey, sitekey,
secret, secret,