Skip to content

Commit adb2822

Browse files
committed
added login module
modified: uploader/CMakeLists.txt modified: uploader/lib/libwiki.cpp new file: uploader/test/login.cpp
1 parent 66310ff commit adb2822

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

uploader/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ target_link_libraries(wikibot PUBLIC nlohmann_json::nlohmann_json curlpp curl)
1717
add_executable(.token test/token.cpp)
1818
target_link_libraries(.token wikibot)
1919
add_test(NAME token COMMAND .token)
20+
add_executable(.login test/login.cpp)
21+
target_link_libraries(.login wikibot)
22+
add_test(NAME login COMMAND .login)

uploader/lib/libwiki.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ std::string wiki::login(const std::string &url,const std::string &cookie_path,co
3838
request.setOpt<curlpp::Options::CookieFile>(cookie_path);
3939
request.setOpt<curlpp::Options::CookieJar>(cookie_path);
4040
curlpp::Forms form;
41+
form.push_back(new curlpp::FormParts::Content("format","json"));
4142
form.push_back(new curlpp::FormParts::Content("action","login"));
4243
form.push_back(new curlpp::FormParts::Content("lgname",username));
4344
form.push_back(new curlpp::FormParts::Content("lgpassword",password));
4445
form.push_back(new curlpp::FormParts::Content("lgtoken",login_token));
4546
request.setOpt<curlpp::Options::HttpPost>(form);
4647
request.setOpt<curlpp::Options::HttpHeader>(header);
47-
return wiki::get(url);
48+
return wiki::get(request);
4849
}

uploader/test/login.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<cstdlib>
2+
#include<exception>
3+
#include<format>
4+
#include<iostream>
5+
#include<string>
6+
#include<nlohmann/json.hpp>
7+
#include"wiki.hpp"
8+
int main(){
9+
try{
10+
const std::string api="http://localhost/api.php?";
11+
const std::string login_token=wiki::get_token(api+"format=json&action=query&meta=tokens&type=login","/tmp/wiki_login_token");
12+
const nlohmann::json result=nlohmann::json::parse(wiki::login(api+"format=json&action=login","/tmp/wiki_login_token","CppHusky@uploader",std::getenv("WIKIPASS_UPLOADER"),login_token));
13+
assert(result["login"]["result"]=="Success");
14+
}catch(const curlpp::RuntimeError &e){
15+
std::cerr<<e.what()<<std::endl;
16+
assert(false);
17+
}catch(const curlpp::LogicError &e){
18+
std::cerr<<e.what()<<std::endl;
19+
assert(false);
20+
}catch(const std::exception &e){
21+
std::cerr<<e.what()<<std::endl;
22+
assert(false);
23+
}
24+
}

0 commit comments

Comments
 (0)