Skip to content

Commit

Permalink
fixed some errors in tx generation and processing
Browse files Browse the repository at this point in the history
  • Loading branch information
alunev committed Jun 2, 2018
1 parent 314b39a commit 7e37547
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 72 deletions.
9 changes: 6 additions & 3 deletions web/app/WebModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import auth.FmPlayAuthResolver;
import auth.FmPlayAuthUserService;
import auth.basic.AlwaysValidBasicAuthProvider;
import auth.deadbolt.FmPlayAuthCustomDeadboltHook;
import auth.deadbolt.FmPlayAuthDeadboltHandler;
import auth.deadbolt.FmPlayAuthHandlerCache;
Expand Down Expand Up @@ -46,13 +47,15 @@ public void configure() {
bind(UserDao.class).asEagerSingleton();

bind(Resolver.class).to(FmPlayAuthResolver.class);
bind(FmPlayAuthUserService.class).asEagerSingleton();
bind(GoogleAuthProvider.class).asEagerSingleton();

bind(DeadboltHandler.class).to(FmPlayAuthDeadboltHandler.class);

bind(FmPlayAuthUserService.class).asEagerSingleton();
bind(FmPlayAuthHandlerCache.class).asEagerSingleton();
bind(FmPlayAuthCustomDeadboltHook.class).asEagerSingleton();

bind(GoogleAuthProvider.class).asEagerSingleton();
bind(AlwaysValidBasicAuthProvider.class).asEagerSingleton();

bind(TransactionExecutor.class).to(LoggingTransactionExecutor.class);
bind(AccountMatcher.class).to(RegexAccountMatcher.class);
bind(TransactionGenerator.class).to(ParsingTransactionGenerator.class);
Expand Down
29 changes: 29 additions & 0 deletions web/app/auth/AuthUserAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package auth;

import com.feth.play.module.pa.providers.oauth2.google.GoogleAuthUser;
import com.feth.play.module.pa.providers.password.UsernamePasswordAuthUser;
import com.feth.play.module.pa.user.AuthUser;

/**
* Created by admin on 10/26/2016.
*/
public class AuthUserAccessor {

public static String getEmailOf(AuthUser authUser) {
if ("google".equals(authUser.getProvider())) {
return ((GoogleAuthUser) authUser).getEmail();
} else if ("basic".equals(authUser.getProvider())) {
return ((UsernamePasswordAuthUser) authUser).getEmail();
} else {
return null;
}
}

private static GoogleAuthUser toGoogleAuthUser(AuthUser authUser) {
if ("google".equals(authUser.getProvider())) {
return (GoogleAuthUser) authUser;
} else {
return null;
}
}
}
17 changes: 0 additions & 17 deletions web/app/auth/AuthUserConverter.java

This file was deleted.

9 changes: 2 additions & 7 deletions web/app/auth/FmPlayAuthUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ public FmPlayAuthUserService(final PlayAuthenticate auth, UserDao userDao) {

@Override
public String save(final AuthUser authUser) {
GoogleAuthUser googleAuthUser = AuthUserConverter.toGoogleAuthUser(authUser);
if (googleAuthUser == null) {
return null;
}

String authUserId = googleAuthUser.getId();
String authUserId = authUser.getId();

User user = userDao.findByAuthId(authUserId);
if (user == null) {
user = User.createEmptyUser(googleAuthUser.getId(), googleAuthUser.getEmail());
user = User.createEmptyUser(authUser.getId(), AuthUserAccessor.getEmailOf(authUser));
userDao.save(user);
}

Expand Down
58 changes: 58 additions & 0 deletions web/app/auth/basic/AlwaysValidBasicAuthProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright © 2014 Florian Hars, nMIT Solutions GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package auth.basic;

import com.feth.play.module.pa.PlayAuthenticate;
import com.feth.play.module.pa.providers.password.DefaultUsernamePasswordAuthUser;
import com.feth.play.module.pa.providers.wwwauth.basic.BasicAuthProvider;
import com.feth.play.module.pa.user.AuthUser;
import play.inject.ApplicationLifecycle;
import play.mvc.Http.Context;
import play.twirl.api.Content;
import views.html.login;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
* A really simple basic auth provider that accepts one hard coded user
*/
@Singleton
public class AlwaysValidBasicAuthProvider extends BasicAuthProvider {

@Inject
public AlwaysValidBasicAuthProvider(final PlayAuthenticate auth, final ApplicationLifecycle lifecycle) {
super(auth, lifecycle);
}

@Override
protected AuthUser authenticateUser(String username, String password) {
return new DefaultUsernamePasswordAuthUser(password, username);
}

@Override
public String getKey() {
return "basic";
}

/**
* Diplay the normal login form if HTTP authentication fails
*/
@Override
protected Content unauthorized(Context context) {
return login.render(this.auth, null, "");
}
}
12 changes: 10 additions & 2 deletions web/app/controllers/RestApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import core.TransactionGenerator;
import dao.SmsDao;
import dao.TransactionDao;
import dao.UserDao;
import model.Sms;
import model.User;
import play.libs.Json;
Expand All @@ -29,24 +30,31 @@ public class RestApiController extends Controller {
private TransactionGenerator transactionGenerator;
private TransactionExecutor transactionExecutor;
private UserService userService;
private UserDao userDao;

@Inject
public RestApiController(SmsDao smsDao,
TransactionDao transactionDao,
ParsingTransactionGenerator transactionGenerator,
TransactionExecutor transactionExecutor,
UserService userService) {
UserService userService, UserDao userDao) {
this.smsDao = smsDao;
this.transactionDao = transactionDao;
this.transactionGenerator = transactionGenerator;
this.transactionExecutor = transactionExecutor;
this.userService = userService;
this.userDao = userDao;
}

public CompletionStage<Result> processSms() {
User user = userService.getUser(session());
Sms sms = Json.fromJson(request().body().asJson(), Sms.class);

User user = userDao.findById(sms.getOwnerId());

if (user == null) {
return CompletableFuture.supplyAsync(() -> internalServerError("No user found with ID " + sms.getOwnerId()));
}

// push message to persistent queue and then async:
// - saving to mongo (for history and traceability)
// - generating of transactions based on sms data
Expand Down
3 changes: 3 additions & 0 deletions web/app/core/ParsingTransactionGenerator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package core;

import common.DateUtils;
import core.message.ParserSelector;
import core.message.RegexBankMatcher;
import core.message.matcher.AccountMatcher;
Expand All @@ -16,6 +17,7 @@
import play.Logger;

import javax.inject.Inject;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -59,6 +61,7 @@ public List<Transaction> generate(Sms sms, User user) {
builder.sourceAmount(result.getAmount());
builder.destAmount(result.getAmount());
builder.categoryId(categoryMatcher.getBestMatch(result.getPayeeString()).getId());
builder.addedTime(DateUtils.now());

switch (result.getTransactionType()) {
case EXPENSE:
Expand Down
17 changes: 13 additions & 4 deletions web/app/model/MessagePattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import org.jongo.marshall.jackson.oid.MongoObjectId;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -36,10 +37,7 @@ public MessagePattern(@JsonProperty("id") String _id,
this.createdTs = createdTs;
}

public MessagePattern(@JsonProperty("ownerId") String ownerId,
@JsonProperty("regex") String regex,
@JsonProperty("bankName") String bankName,
@JsonProperty("createdTs") LocalDateTime createdTs) {
public MessagePattern(String ownerId, String regex, String bankName, LocalDateTime createdTs) {
this.ownerId = ownerId;
this.regex = regex;
this.bankName = bankName;
Expand All @@ -65,4 +63,15 @@ public String getBankName() {
public LocalDateTime getCreatedTs() {
return createdTs;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("_id", _id)
.add("ownerId", ownerId)
.add("regex", regex)
.add("bankName", bankName)
.add("createdTs", createdTs)
.toString();
}
}
77 changes: 42 additions & 35 deletions web/conf/play-authenticate/mine.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,46 @@

play-authenticate {

# If set to true, account merging is enabled, if set to false its disabled and accounts will never prompted to be merged
# defaults to true
accountMergeEnabled=false

# if this is set to true, accounts are automatically linked
# (e.g. if a user is logged in and uses a different authentication provider
# which has NOT yet been registered to another user, this newly used authentication
# provider gets added to the current local user
# Handle setting this to true with care
# If set to false, your resolver must not return null for askLink()
# defaults to false
accountAutoLink=true

# Settings for the google-based authentication provider
# if you are not using it, you can remove this portion of the config file
# and remove the Google provider from conf/play.plugins
google {
redirectUri {
# Whether the redirect URI scheme should be HTTP or HTTPS (HTTP by default)
secure=false

# You can use this setting to override the automatic detection
# of the host used for the redirect URI (helpful if your service is running behind a CDN for example)
# host=yourdomain.com
}

# Google credentials
# These are mandatory for using OAuth and need to be provided by you,
# if you want to use Google as an authentication provider.
# Get them here: https://code.google.com/apis/console
# Remove leading '#' after entering

clientId = ${GOOGLE_OAUTH_CLIENT_ID}
clientSecret = ${GOOGLE_OAUTH_SECRET}
# If set to true, account merging is enabled, if set to false its disabled and accounts will never prompted to be merged
# defaults to true
accountMergeEnabled = false

# if this is set to true, accounts are automatically linked
# (e.g. if a user is logged in and uses a different authentication provider
# which has NOT yet been registered to another user, this newly used authentication
# provider gets added to the current local user
# Handle setting this to true with care
# If set to false, your resolver must not return null for askLink()
# defaults to false
accountAutoLink = true

# Settings for the google-based authentication provider
# if you are not using it, you can remove this portion of the config file
# and remove the Google provider from conf/play.plugins
google {
redirectUri {
# Whether the redirect URI scheme should be HTTP or HTTPS (HTTP by default)
secure = false

# You can use this setting to override the automatic detection
# of the host used for the redirect URI (helpful if your service is running behind a CDN for example)
# host=yourdomain.com
}
}

# Google credentials
# These are mandatory for using OAuth and need to be provided by you,
# if you want to use Google as an authentication provider.
# Get them here: https://code.google.com/apis/console
# Remove leading '#' after entering

clientId = ${GOOGLE_OAUTH_CLIENT_ID}
clientSecret = ${GOOGLE_OAUTH_SECRET}
}

# Settings for the http basic auth provider
# if you are not using it (and you shouldn't), you can remove this portion
# of the config file
basic {
realm=Play_Authenticate
}
}
7 changes: 6 additions & 1 deletion web/test/DataPopulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void setUp() {
public void populatePatterns(String ownerId) {
patternDao.save(new MessagePattern(
ownerId,
"Покупка\\. Карта \\*(\\d{4}). (\\d+\\.\\d+) RUB. (.*). Доступно (\\d+\\.\\d+) RUB",
"(.*)\\. Карта \\*(\\d{4}). (\\d+\\.\\d+) (.*). OKEY. Доступно (\\d+\\.\\d+) RUB",
"Tinkoff",
DateUtils.now()
));
Expand All @@ -70,6 +70,7 @@ public void populateUserWithSingleAccoutAndSomeTransactions() {
TransactionCategory flatCat = TransactionCategory.createTransactionCategory("Flat", "commodities");
TransactionCategory transportCat = TransactionCategory.createTransactionCategory("Transaport", "tickets etc.");

transactionCategoryDao.save(TransactionCategory.UNDEFINED);
transactionCategoryDao.save(foodCat);
transactionCategoryDao.save(flatCat);
transactionCategoryDao.save(transportCat);
Expand All @@ -83,6 +84,10 @@ public void populateUserWithSingleAccoutAndSomeTransactions() {
Account account = ObjectsFactory.createDummyAccountWithOwnerId(user.getId());
accountDao.save(account);

accountDao.save(Account.EXPENSE_ACCOUNT);
accountDao.save(Account.INCOME_ACCOUNT);
accountDao.save(Account.UNDEFINED_ACCOUNT);

user.addAccount(account);

transactionDao.save(Transaction.createExpense(
Expand Down
2 changes: 1 addition & 1 deletion web/test/controllers/RestApiControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void sendSomeSmses() throws Exception {
"",
"android-1",
"",
"Покупка Карта *2222. 42.42 RUB. OKEY DOSTAVKA. Доступно 1325.50 RUB",
"Покупка. Карта *2222. 3344.5 RUB. OKEY. Доступно 12345.92 RUB",
ZonedDateTime.now(ZoneOffset.UTC).toLocalDateTime());

JsonNode jsonNode = Json.toJson(sms);
Expand Down
2 changes: 1 addition & 1 deletion web/test/core/message/RegexBankMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void canMatchBank() {
RegexBankMatcher bankMatcher = new RegexBankMatcher(Lists.newArrayList(
new MessagePattern(
"user01",
"Покупка\\. Карта \\*(\\d{4}). (\\d+\\.\\d+) RUB. (.*). Доступно (\\d+\\.\\d+) RUB",
"(.*)\\. Карта \\*(\\d{4}). (\\d+\\.\\d+) (.*). OKEY. Доступно (\\d+\\.\\d+) RUB",
"bank1",
DateUtils.now()
),
Expand Down
3 changes: 2 additions & 1 deletion web/test/dao/MessagePatternDaoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dao;

import com.google.common.collect.Sets;
import common.DateUtils;
import model.Account;
import model.MessagePattern;
Expand Down Expand Up @@ -43,6 +44,6 @@ public void canFindByOwnerId() {

patternDao.save(pattern);

assertThat(patternDao.findByOwnerId("01")).isEqualTo(pattern);
assertThat(patternDao.findByOwnerId("user01").stream().findFirst().get().getId()).isEqualTo(pattern.getId());
}
}

0 comments on commit 7e37547

Please sign in to comment.